Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

grep and array

by amoura (Initiate)
on Jul 09, 2002 at 13:40 UTC ( [id://180466]=perlquestion: print w/replies, xml ) Need Help??

amoura has asked for the wisdom of the Perl Monks concerning the following question:

hello guys , I have this pice of my script:
open(OUTother, "< C:/Perl/MyScripts/tmp.txt") || die "Could not open file : $!\n"; my @hold2; while (<OUTother>) { push(@hold2, $1) && last if (m/(\w+\.bld)/); } `egrep -wn @hold2 nbsssbs//esel/bld/*.bld nbsssbs/sbc/bld/*.bl +d nbssbts/btsc/bld/*.bld > $temp/otherblds2.txt `; close(OUTother) or die("Can't close file: $!");
I have problem with the grep , It doesn't like or not allow me to grep a value inside an array , is threre away to go around this , once I excuted , I get no error but no file is generated for the output "otherblds2.txt".

2002-07-09 Edit by Corion : Added formatting

Replies are listed 'Best First'.
Re: grep and array
by Aristotle (Chancellor) on Jul 09, 2002 at 15:20 UTC
    You're using an awfully complicated method to pick the desired filenames.. The following will suffice: my @hold2 = map /(\w+\.bld)/, <OUTother>; Note however that egrep expects only a single, first argument to be an expression to search for. So it interprets the array elements of @hold2 other than the first one as names of files to examine.. what you want to do is to construct a regex:
    my $hold2 = join "|", @hold2; `egrep -wn '$hold2' nbsssbs//esel/bld/*.bld nbsssbs/sbc/bld/*.bld nbss +bts/btsc/bld/*.bld > $temp/otherblds2.txt `;
    Which you can roll into the map at the top. That makes: my $hold2 = join "|", map /(\w+\.bld)/, <OUTother>; But why use backticks when you're throwing the program's output away anyway? system "egrep -wn '$hold2' nbsssbs//esel/bld/*.bld nbsssbs/sbc/bld/*.bld nbssbts/btsc/bld/*.bld > $temp/otherblds2.txt" or die "egrep failed: $!"; Since you're outputting the results to a file in a tempdir, I suspect you go on to read them back in later - that would be an awfully roundabout way of doing what you want. Instead, you can open a pipe to it:
    open EGREP, "egrep -wn '$hold2' nbsssbs//esel/bld/*.bld nbsssbs/sbc/bl +d/*.bld nbssbts/btsc/bld/*.bld |" or die "Pipe to egrep failed: $!"; my @othersblds = <EGREP>; close EGREP;
    Now your desired data should be in @otherblds. That gives us:
    open(OUTother, "<", "C:/Perl/MyScripts/tmp.txt") or die "Couldn't open + file: $!"; my $hold2 = join "|", map /(\w+\.bld)/, <OUTother>; close OUTother; open EGREP, "egrep -wn '$hold2' nbsssbs//esel/bld/*.bld nbsssbs/sbc/bl +d/*.bld nbssbts/btsc/bld/*.bld |" or die "Pipe to egrep failed: $!"; my @othersblds = <EGREP>; close EGREP;
    Of course if you really do want the tempfile, use the system call instead.

    Makeshifts last the longest.

Re: grep and array
by fglock (Vicar) on Jul 09, 2002 at 14:17 UTC

    try (untested):

    my $hold = join(' ', @hold2);

    Then use $hold instead of @hold2

    `egrep -wn  $hold nbsssbs//esel/bld/*.bld nbsssbs/sbc/bld/*.bld nbssbts/btsc/bld/*.bld > $temp/otherblds2.txt `;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180466]
Approved by little
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found