Perlwanab has asked for the wisdom of the Perl Monks concerning the following question:
I'm opening and reading in 30.txt to <SOURCE> filehandle. I open searchfile.txt to <FILE2SEARCH> and assigned the elements to @sfile and then close <FILE2SEARCH>. I read in SOURCE one record at a time and assign it to $script, chomp the new line off $script then I want to grep $script for each element of @sfile. When the first record of <SOURCE> is done, I want to take the next record from <SOURCE> and grep that against @sfile and so on. For each statement that is true, I want the element from @sfile to be assigned to @line. Instead of @line having only those elements from @sfile matching each $script variable, @line is a duplicate copy of @sfile. What am I missing?
You don't see it below, but I did some testing to see if each record of <SOURCE> is being recognized. So after chomp = $script; I added a line to print each $script value and it printed each $script variable.
#!/usr/bin/perl -w open(SOURCE, "30.txt") || die "Cannot open: $!"; open(FILE2SEARCH, "searchfile.txt") || die "Cannot open: $!"; @sfile=<FILE2SEARCH>; close(FILE2SEARCH); while (<SOURCE>) { $script = <SOURCE>; chomp $script; <p> the next line is where my problem begins. I have warning on, but I + get no errors.</p> @line=grep( /$script/, @sfile ); } close(SOURCE); open(DEST, ">>newfile.txt") or die "Can't open new.cfg: $!"; print DEST @line; close(DEST);
This is what 30.txt looks like big.script.sh onetime.scrip.sh pay.sh scripta.sh scripta.1.sh scriptbb.sh scriptbb.1.sh scriptbb.2.sh
This is what searchfile.txt looks like file="^billing.file*" id=none synccmd="/foo/bin/scriptbb2.sh + %P %D %F" file="^pay.file*" id=none synccmd="/foo/bin/pay.sh %P %D + %F" file="^car.file*" id=none synccmd="/foo/bin/big.script.s +h %P %D %F" file="^last.file*" id=none synccmd="/foo/bin/nowhere.scri +pt.sh %P %D %F" file="^grass.file*" id=none synccmd="/foo/bin/grass.script +.sh %P %D %F" file="^cart.file*" id=none synccmd="/foo/bin/cart.script. +sh %P %D %F" file="^mortgage.file*" id=none synccmd="/foo/bin/big.script.s +h %P %D %F" file="^lincoln.file*" id=none synccmd="/foo/bin/onetime.scri +pt.sh %P %D %F" file="^music.file*" id=none synccmd="/foo/bin/scripta.sh % +P %D %F" file="^house.file*" id=none synccmd="/foo/bin/scripta.1.sh + %P %D %F" file="^garage.file*" id=none synccmd="/foo/bin/scripta.1.sh + %P %D %F" file="^tree.file*" id=none synccmd="/foo/bin/scriptbb.1.s +h %P %D %F" file="^foo.file*" id=none synccmd="/foo/bin/scriptbb.2.s +h %P %D %F" file="^fun.file*" id=none synccmd="/foo/bin/notthis.sh % +P %D %F" file="^done.file*" id=none synccmd="/foo/bin/donethis.sh +%P %D %F" file="^cement.file*" id=none synccmd="/foo/bin/cement.sh %P + %D %F" file="^animal.file*" id=none synccmd="/foo/bin/scripta.sh % +P %D %F"
This is what @line shoud look like after the grep file="^car.file*" id=none synccmd="/foo/bin/big.script.s +h %P %D %F" file="^mortgage.file*" id=none synccmd="/foo/bin/big.script.s +h %P %D %F" file="^lincoln.file*" id=none synccmd="/foo/bin/onetime.scri +pt.sh %P %D %F" file="^pay.file*" id=none synccmd="/foo/bin/pay.sh %P %D + %F" file="^music.file*" id=none synccmd="/foo/bin/scripta.sh % +P %D %F" file="^animal.file*" id=none synccmd="/foo/bin/scripta.sh % +P %D %F" file="^house.file*" id=none synccmd="/foo/bin/scripta.1.sh + %P %D %F" file="^garage.file*" id=none synccmd="/foo/bin/scripta.1.sh + %P %D %F" file="^tree.file*" id=none synccmd="/foo/bin/scriptbb.1.s +h %P %D %F" file="^foo.file*" id=none synccmd="/foo/bin/scriptbb.2.s +h %P %D %F"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: grep array elemetns against another array
by ikegami (Patriarch) on Oct 11, 2009 at 21:07 UTC | |
|
Re: Perlwanab grep array elements against another array
by moritz (Cardinal) on Oct 11, 2009 at 20:50 UTC | |
|
Re: Perlwanab grep array elemetns against another array
by biohisham (Priest) on Oct 12, 2009 at 10:35 UTC |