For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > grep usage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author grep usage
Reginald Johnson

2008-03-26, 7:11 pm

I am trying to grep an array return the lines that match. From my
reading I see that grep returns the number of times an expression was
true not the actual expression. My question is how would I get the
actual expression.
I tested my code with a small input file of 2 records. The first one is
in the @mhsarray the second one is not.
I am trying to get my output file to have "CISCSTT6....line from
@mhsarray that it matched".

$ cat mhs.pl
#!/usr/bin/perl
use warnings;

$file="/adsm/nodes";
$mhs="/adsm/mhs_alloc";
$file_out="/adsm/in_mhs";

open (INFILE, "<", "$file") or
die "$file could not be opened: $!";
open (MHSFILE, "<", "$mhs") or
die "$mhs could not be opened: $!";
open (OUTFILE, ">", "$file_out") or
die "$file_out could not be opened: $!";

while (<MHSFILE> ) {
chomp($_);
push (@mhsArray, $_);
}

foreach $line (<INFILE> ) {
chomp($line);
print "this is line $line\n";
@inmhs = grep(/$line/,@mhsArray);
print "size of inmhs array is $#inmhs\n";
if ($#inmhs > -1) {
print (OUTFILE "$line\n"); # would like to be
$line...line from @mhsArray
# print "$_\n";
}
} #end foreach
close(INFILE);
close(MHSFILE);
close(OUTFILE);
cicstt6 () [ /a
Reggie Johnson
TSM Admin
904.218.4620
Jacksonville, Fl
--------------------------------------------------------

This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
--------------------------------------------------------

Yitzle

2008-03-26, 7:11 pm

Have you tried it?
Grep does return the result lines when used in a list context, ie how
you used it
> @inmhs = grep(/$line/,@mhsArray);

print "$_\n" for (@inmhs);

From Perldoc:
"Evaluates the BLOCK or EXPR for each element of LIST (locally setting
$_ to each element) and returns the list value consisting of those
elements for which the expression evaluated to true. In scalar
context, returns the number of times the expression was true."
John W. Krahn

2008-03-26, 7:11 pm

Johnson, Reginald (GTI) wrote:
> I am trying to grep an array return the lines that match. From my
> reading I see that grep returns the number of times an expression was
> true not the actual expression. My question is how would I get the
> actual expression.
> I tested my code with a small input file of 2 records. The first one is
> in the @mhsarray the second one is not.
> I am trying to get my output file to have "CISCSTT6....line from
> @mhsarray that it matched".
>
> $ cat mhs.pl
> #!/usr/bin/perl
> use warnings;


use strict;


> $file="/adsm/nodes";
> $mhs="/adsm/mhs_alloc";
> $file_out="/adsm/in_mhs";


my $file = '/adsm/nodes';
my $mhs = '/adsm/mhs_alloc';
my $file_out = '/adsm/in_mhs';


> open (INFILE, "<", "$file") or
> die "$file could not be opened: $!";
> open (MHSFILE, "<", "$mhs") or
> die "$mhs could not be opened: $!";
> open (OUTFILE, ">", "$file_out") or
> die "$file_out could not be opened: $!";


perldoc -q quoting


> while (<MHSFILE> ) {
> chomp($_);
> push (@mhsArray, $_);
> }


Or simply:

chomp( my @mhsArray = <MHSFILE> );


> foreach $line (<INFILE> ) {


while ( my $line = <INFILE> ) {


> chomp($line);
> print "this is line $line\n";
> @inmhs = grep(/$line/,@mhsArray);


$line may contain regexp meta-characters so you should quotemeta() it:

my @inmhs = grep /\Q$line/, @mhsArray;

Or did you really want to check for equality:

my @inmhs = grep $_ eq $line, @mhsArray;


If you just want the number of times that $line matches @mhsArray then
use a scalar:

my $inmhs_count = grep /\Q$line/, @mhsArray;


> print "size of inmhs array is $#inmhs\n";


$#inmhs is *not* the size of @inmhs, $#inmhs is the index of the last
element of @inmhs. @inmhs in scalar context is the size of @inmhs.

print "size of inmhs array is ", scalar @inmhs, "\n";


> if ($#inmhs > -1) {


Just use @inmhs in scalar context:

if ( @inmhs > 0 ) {

Or simply:

if ( @inmhs ) {


> print (OUTFILE "$line\n"); # would like to be $line...line from @mhsArray
> # print "$_\n";
> }
> } #end foreach
> close(INFILE);
> close(MHSFILE);
> close(OUTFILE);



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com