while grep filehandle

I

incognito

Unix command line:

grep Results File.txt

Returns lots of lines:

Results = 1
Results = 11
Results = 2

etc, etc, etc.

Perl script:

#!/usr/bin/perl

open (IN, "< File.txt");

while ( grep /Results/, <IN> ) {
print "$_\n";
}

close (IN);

Returns nothing. Why?
 
P

Paul Lalli

Unix command line:

grep Results File.txt

Returns lots of lines:

Results = 1
Results = 11
Results = 2

etc, etc, etc.

Perl script:

#!/usr/bin/perl

open (IN, "< File.txt");

while ( grep /Results/, <IN> ) {
print "$_\n";
}

close (IN);

Returns nothing. Why?


Basically because you're not using grep correctly. You're using grep in
the condition to a while. That means you're using it in scalar context.
In a scalar context, grep returns the number of times its condition
statement (in this case: /Results/ ) returned true. $_ is never set
within the while loop.

What you want is to return the lines that grep matched correctly:

my @lines = grep /Results/, <IN>;
print @lines;

Or even just:

print grep /Results/, <IN>;

If you're going to use a while loop, you shouldn't be using grep:

while (<IN>){
print if /Results/;
}


Or you could just simplify this into a oneliner:

perl -ne 'print if /Results/' File.txt


By the way, I'm pretty sure if you had enabled warnings, you would have
gotten an uninitialize variable warning for the $_ inside the while loop.
That would have given you a clue as to what was going wrong. Please
enable warnings in the future before posting.

Paul Lalli
 
J

Juha Laiho

(e-mail address removed) (incognito) said:
#!/usr/bin/perl

open (IN, "< File.txt");

while ( grep /Results/, <IN> ) {
print "$_\n";
}

close (IN);

Returns nothing. Why?

My brain fails as to why the above doesn't work, but I tried the
following and it looks ok (though didn't test with a big input
file):

#!/usr/bin/perl
use warnings;
use strict;

open (IN, "< File.txt");
print grep /Results/, <IN>;
close (IN);
 
P

Paul Lalli

(e-mail address removed) (incognito) said:

My brain fails as to why the above doesn't work,

read perldoc -f grep to understand what grep does in a scalar context.
but I tried the
following and it looks ok (though didn't test with a big input
file):

#!/usr/bin/perl
use warnings;
use strict;

open (IN, "< File.txt");
print grep /Results/, <IN>;
close (IN);

This is correct, because the print unary operator takes a list, so grep
here is being called in a list context.

Note that this will be a memory hog for large files, because all lines
will be read and stored, rather than one at a time. If this is a concern,
it is better written as:
while (<IN>){
print if /Results/;
}


See my other post in this thread for the method of doing this in a perl
one-liner.

Paul Lalli
 
J

Joe Smith

Juha said:
My brain fails as to why the above doesn't work,

There's a big difference between
foreach ( grep /Results/, <IN>) { print }
and
while ( grep /Results/, <IN>) { print }

-Joe
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

Unix command line:

grep Results File.txt

Returns lots of lines:

Results = 1
Results = 11
Results = 2

etc, etc, etc.

Perl script:

#!/usr/bin/perl

open (IN, "< File.txt");

while ( grep /Results/, <IN> ) {
print "$_\n";
}

close (IN);

Returns nothing. Why?

You are not using grep correctly. The result returned by grep is not a
TRUE/FALSE value intended to be used as a logical test in a conditional
statement, but rather is an array containing the matched entries.


#!/usr/bin/perl

use warnings;
use strict;

my ( $filename , @matches );

$filename = "File.txt";
open(IN,"<$filename") or
die("Can't open $filename : $!\n");

@matches = grep(/Results/,<IN>);
close IN;
print @matches;

exit 0;
 
B

Ben Morrow

Quoth Web Surfer said:
[This followup was posted to comp.lang.perl.misc]

I know, that's where I read it.
You are not using grep correctly. The result returned by grep is not a
TRUE/FALSE value intended to be used as a logical test in a conditional
statement, but rather is an array containing the matched entries.

In list context, yes; in scalar context, grep returns the number of
matches, which when treated as a boolean gives if there were any at all.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top