Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > while grep filehandle

Reply
Thread Tools

while grep filehandle

 
 
incognito
Guest
Posts: n/a
 
      06-18-2004
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?
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-18-2004
On Fri, 18 Jun 2004, incognito wrote:

> 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

 
Reply With Quote
 
 
 
 
Juha Laiho
Guest
Posts: n/a
 
      06-18-2004
http://www.velocityreviews.com/forums/(E-Mail 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);
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      06-18-2004
On Fri, 18 Jun 2004, Juha Laiho wrote:

> (E-Mail 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,


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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-18-2004
Juha Laiho wrote:

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

>
> 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
 
Reply With Quote
 
Web Surfer
Guest
Posts: n/a
 
      06-19-2004
[This followup was posted to comp.lang.perl.misc]

In article <(E-Mail Removed)> ,
(E-Mail Removed) says...
> 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;
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-19-2004

Quoth Web Surfer <(E-Mail Removed)>:
> [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

--
Joy and Woe are woven fine,
A Clothing for the Soul divine William Blake
Under every grief and pine 'Auguries of Innocence'
Runs a joy with silken twine. (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Commenting within Filehandle Iteration Wijaya Edward Python 1 10-26-2006 08:18 AM
key as filehandle error Bill Perl 2 09-01-2004 12:22 PM
$v = shift OR$_[0] on a filehandle, whats teh difereence Eduard W. Lohmann Perl 1 12-02-2003 06:30 PM
Behaviour of filehandle.readlines() for images on Windows Jorge Godoy Python 2 10-14-2003 08:35 PM
HELP! using filehandle.get("filename") writes to file when it reads!? Christopher Reeve C++ 1 09-14-2003 08:18 PM



Advertisments