Re: perl grep problem
- From: "Mark Clements" <mark.clementsREMOVETHIS@xxxxxxxxxx>
- Date: 25 May 2005 14:12:04 GMT
demolitionz@xxxxxxxxx wrote:
> 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?! :)
>
You need to post a small, complete program that displays this
behaviour, as well as sample data and output, copying and pasting
rather than retyping. Check out the posting guidelines. I would have
suggested "chomp" rather than "chop", but you've tried that. Is also
possible that the data you are feeding to STDIN has something
unexpected in it. Bear in mind that you'll need to escape special
characters if you want to use them in a regex to match the, er, special
characters.
Mark
.
- Follow-Ups:
- Re: perl grep problem
- From: demolitionz
- Re: perl grep problem
- References:
- perl grep problem
- From: demolitionz
- perl grep problem
- Prev by Date: Re: fibonacci string
- Next by Date: Re: Compiling to exe
- Previous by thread: perl grep problem
- Next by thread: Re: perl grep problem
- Index(es):
Relevant Pages
|