D
demolitionz
hey, wonder if anyone can help 'cause i'm fresh out of ideas why my
perl script isn't working!
basically the script reads all the data from files in a directory into
an array. i then want the user to be able to search that array for
keywords (in each line) and output the keywords to a file. i've got
the script to work using the following line:
@found = grep(/$ARGV[2]/i, @rf);
where @rf is the array that's being searched, @found is the array the
found words are stored to (i output it to a file later which also works
fine) and $ARGV[2] is the user input word to search for. the problem
with this script is that because the user inputs the search word as
$ARGV[2] the program can only search for one word per run, which means
when they want to search for another word they have to run the whole
program again and this slows things down as the @rf array has to be
created from scratch once more.
what i want to do (and what i've tried endlessly to do in the 2nd
remake of the script!) is to have a 2 step proccess, where the files
are read into the @rf array as step one, and then in step 2 the user
inputs the keyword to search for and we loop step 2 as many times as
the user wants. what i'm currently doing with that then, is this:
$keyword = <STDIN>;
chop $keyword;
@found = grep(/$keyword/i, @rf);
now i've printed to screen everything so as to debug it, and if the
user inputs "chickens" for example, then print $keyword; will return
"chickens" correctly. the problem is, no matter what i try, i cannot
get the grep(/$keyword/) bit to work and @found is *always* empty! i
don't really understand why grep would work fine with $ARGV[2] but not
with $keyword and it's drivin me crazy! i've tried @found =
grep(/"$keyword"/i, @rf); and i've tried chomp $keyword; and i've even
resorted to pushing $keyword into an array and calling the same value
from the array as a scalar (i got very very desperate by this point and
would try anything ) but nothing i do works.
can anyone help?!
cheers,
d
perl script isn't working!
basically the script reads all the data from files in a directory into
an array. i then want the user to be able to search that array for
keywords (in each line) and output the keywords to a file. i've got
the script to work using the following line:
@found = grep(/$ARGV[2]/i, @rf);
where @rf is the array that's being searched, @found is the array the
found words are stored to (i output it to a file later which also works
fine) and $ARGV[2] is the user input word to search for. the problem
with this script is that because the user inputs the search word as
$ARGV[2] the program can only search for one word per run, which means
when they want to search for another word they have to run the whole
program again and this slows things down as the @rf array has to be
created from scratch once more.
what i want to do (and what i've tried endlessly to do in the 2nd
remake of the script!) is to have a 2 step proccess, where the files
are read into the @rf array as step one, and then in step 2 the user
inputs the keyword to search for and we loop step 2 as many times as
the user wants. what i'm currently doing with that then, is this:
$keyword = <STDIN>;
chop $keyword;
@found = grep(/$keyword/i, @rf);
now i've printed to screen everything so as to debug it, and if the
user inputs "chickens" for example, then print $keyword; will return
"chickens" correctly. the problem is, no matter what i try, i cannot
get the grep(/$keyword/) bit to work and @found is *always* empty! i
don't really understand why grep would work fine with $ARGV[2] but not
with $keyword and it's drivin me crazy! i've tried @found =
grep(/"$keyword"/i, @rf); and i've tried chomp $keyword; and i've even
resorted to pushing $keyword into an array and calling the same value
from the array as a scalar (i got very very desperate by this point and
would try anything ) but nothing i do works.
can anyone help?!
cheers,
d