freelanceprogrammers.org Forum Index » Perl

how to read the column and pick the row?


View user's profile Post To page top
balajirajes Posted: Thu Sep 08, 2005 11:26 am


Joined: 08 Sep 2005

Posts: 1
how to read the column and pick the row?
I am trying to ignore the zero values in a specific column....

My source file looks like this

002-1241958-363521 B00087L1N 0 1
002-1241958-363521 B007KQWGO 43.94 1
002-1275607-581205 B0005OAZ3 149 0

and would like to take the line out where third and forth column
should not have `0` value and would like to print in a seperate file
like this (the file is having 2 lakh lines)

002-1241958-363521 B007KQWGO 43.94 1

I have written is

#!/opt/third-party/bin/perl -w

#use strict;

my $infile = <STDIN>; chomp;
my $outfile = <STDIN>; chomp;
open FH1, "<$infile" or die "$!";
open FH2, ">$outfile" or die "$!";
while (my $line = <FH1>)
{
my ($col0, $col1, $price, $units) = split(/ /,$line);
if ($units == 0 || $price == 0 ){
NEXT;
}
print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
$line++;
}
close FH1;
close FH2;

not yielding the expected results... Can any one help me on this?
Reply with quote
Send private message
View user's profile Post To page top
golagolu Posted: Thu Sep 08, 2005 7:26 pm


Joined: 19 Mar 2005

Posts: 5
how to read the column and pick the row?
I tried this code and it appears to do the job correctly. The only
modification I did was to add "chop $line" and used "next" instead
of "NEXT".

==================
my $infile ="input.txt";
my $outfile = "output.txt";
open FH1, "<$infile" or die "$!";
open FH2, ">$outfile" or die "$!";
while (my $line = <FH1>)
{
chop $line;
my ($col0, $col1, $price, $units) = split(/ /,$line);
if ($units == 0 || $price == 0 ){
next;
}
print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
$line++;
}
close FH1;
close FH2;
=====================


--- In perl_official@yahoogroups.com, "balajirajes"
<balajirajes@y...> wrote:
> I am trying to ignore the zero values in a specific column....
>
> My source file looks like this
>
> 002-1241958-363521 B00087L1N 0 1
> 002-1241958-363521 B007KQWGO 43.94 1
> 002-1275607-581205 B0005OAZ3 149 0
>
> and would like to take the line out where third and forth column
> should not have `0` value and would like to print in a seperate
file
> like this (the file is having 2 lakh lines)
>
> 002-1241958-363521 B007KQWGO 43.94 1
>
> I have written is
>
> #!/opt/third-party/bin/perl -w
>
> #use strict;
>
> my $infile = <STDIN>; chomp;
> my $outfile = <STDIN>; chomp;
> open FH1, "<$infile" or die "$!";
> open FH2, ">$outfile" or die "$!";
> while (my $line = <FH1>)
> {
> my ($col0, $col1, $price, $units) = split(/ /,$line);
> if ($units == 0 || $price == 0 ){
> NEXT;
> }
> print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
> $line++;
> }
> close FH1;
> close FH2;
>
> not yielding the expected results... Can any one help me on this?
Reply with quote
Send private message
View user's profile Post To page top
nottherat Posted: Thu Sep 08, 2005 8:05 pm


Joined: 03 Jul 2005

Posts: 15
how to read the column and pick the row?
golagolu <> wrote:

: I tried this code and it appears to do the job correctly. The
: only modification I did was to add "chop $line" and ...

For line endings, `chomp` is preferred over `chop`.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
Reply with quote
Send private message
View user's profile Post To page top
skpkrish Posted: Fri Sep 09, 2005 9:07 am


Joined: 13 Aug 2005

Posts: 12
how to read the column and pick the row?
Hai All,

my $infile ="input.txt";
my $outfile = "output.txt";
open FH1, "<$infile" or die "$!";
open FH2, ">$outfile" or die "$!";
while (my $line = <FH1>)
{
chop $line;
my ($col0, $col1, $price, $units) = split(/ /,$line);
if ($units == 0 || $price == 0 ){
next;
}
print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
$line++;
}
close FH1;
close FH2;

In the above code there is a mistake, see chop function removes any character
irrespective of whatsoever the character may be, if the end of the line
character is not a line break, then the character which is not a line break will
be removed, but if you try chomp instead of chop, it will remove only the line
break character, and your code will be effective.

Regards
Krish.


golagolu <no_reply@yahoogroups.com> wrote:
I tried this code and it appears to do the job correctly. The only
modification I did was to add "chop $line" and used "next" instead
of "NEXT".

==================
my $infile ="input.txt";
my $outfile = "output.txt";
open FH1, "<$infile" or die "$!";
open FH2, ">$outfile" or die "$!";
while (my $line = <FH1>)
{
chop $line;
my ($col0, $col1, $price, $units) = split(/ /,$line);
if ($units == 0 || $price == 0 ){
next;
}
print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
$line++;
}
close FH1;
close FH2;
=====================


--- In perl_official@yahoogroups.com, "balajirajes"
<balajirajes@y...> wrote:
> I am trying to ignore the zero values in a specific column....
>
> My source file looks like this
>
> 002-1241958-363521 B00087L1N 0 1
> 002-1241958-363521 B007KQWGO 43.94 1
> 002-1275607-581205 B0005OAZ3 149 0
>
> and would like to take the line out where third and forth column
> should not have `0` value and would like to print in a seperate
file
> like this (the file is having 2 lakh lines)
>
> 002-1241958-363521 B007KQWGO 43.94 1
>
> I have written is
>
> #!/opt/third-party/bin/perl -w
>
> #use strict;
>
> my $infile = <STDIN>; chomp;
> my $outfile = <STDIN>; chomp;
> open FH1, "<$infile" or die "$!";
> open FH2, ">$outfile" or die "$!";
> while (my $line = <FH1>)
> {
> my ($col0, $col1, $price, $units) = split(/ /,$line);
> if ($units == 0 || $price == 0 ){
> NEXT;
> }
> print FH2 $col0, " ", $col1, " ", $price, " ", $units,"
";
> $line++;
> }
> close FH1;
> close FH2;
>
> not yielding the expected results... Can any one help me on this?







SPONSORED LINKS
Basic programming language C programming language Computer programming languages
The c programming language C programming language List of programming languages

---------------------------------
YAHOO! GROUPS LINKS


Visit your group "perl_official" on the web.

To unsubscribe from this group, send an email to:
perl_official-unsubscribe@yahoogroups.com

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


---------------------------------




Regards,

Krish.

---------------------------------
Click here to donate to the Hurricane Katrina relief effort.

[Non-text portions of this message have been removed]
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help