Re: perl grep problem



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
.



Relevant Pages

  • Re: Logon script - function array and select case not working
    ... this all works well, except, the function i am using for the rules in the control script causes alot of querrys to AD. as there are alot of groups. ... objTSout.writeline retrv ... So if you think that this will assign an array value to the variable, how do you think the case select statement is going go compare this array value with the literal string values such as "group name here"? ... However, by not assigning ANY value to checkgrp in the function, you are guaranteeing that, should the function ever exit, it will return no information. ...
    (microsoft.public.scripting.vbscript)
  • perl grep problem
    ... basically the script reads all the data from files in a directory into ... an array. ... inputs the keyword to search for and we loop step 2 as many times as ... user inputs "chickens" for example, ...
    (comp.lang.perl.misc)
  • Re: Perl: Subroutines and use of the "GD::Graph::bars;" Module
    ... I have been looking at the script once again, I could not really get your suggestion to work, but I have been doing some new changes thought. ... @array = sort @array; ... sub verbose { ... Global symbol "$title" requires explicit package name at ./bars.pl line 53. ...
    (perl.beginners)
  • Re: string retrieval issue
    ... Chicago Bears|NFC North ... not writing the third element back to the array). ... You didn't include it in your script. ... Fear is the mind-killer. ...
    (comp.lang.perl.misc)
  • Re: Perl: Subroutines and use of the "GD::Graph::bars;" Module
    ... I did what you told me, but I just gets this error msg in return when I am trying to execute the script: ... And I also get this error msg even when I am not commenting out the settings around line 93? ... @array = sort @array; ... sub verbose { ...
    (perl.beginners)