Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

matching an array

by Anonymous2003 (Initiate)
on Aug 08, 2003 at 12:06 UTC ( [id://282161]=perlquestion: print w/replies, xml ) Need Help??

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

I know that I can't do this way, but it's to explain what I want to do! I have an array containing several values, result of a form, and I want to print in a file the lines containing these values. I try to do sthg like this:
foreach my $paramKey (@params){ $i = 0; open (FILE1, "<tabtext.txt"); open (FILE2, ">ludo.txt"); while(<FILE1>){ if (/$paramKey[$i]/){ print FILE2; } $i++; } close(FILE1); close(FILE2); }

Replies are listed 'Best First'.
Re: matching an array
by AcidHawk (Vicar) on Aug 08, 2003 at 12:22 UTC
Re: matching an array
by flounder99 (Friar) on Aug 08, 2003 at 12:58 UTC
    How about something like this?
    open (FILE1, "<tabtext.txt") or die $!; open (FILE2, ">ludo.txt") or die $!; my $re = join "|", map {quotemeta} @params; while (<FILE1>) { print FILE2 if /$re/o; } close(FILE1); close(FILE2);

    --

    flounder

Re: matching an array
by Zeroth (Beadle) on Aug 08, 2003 at 12:21 UTC
    You might mean like this:
    open (FILE1, "<tabtext.txt"); open (FILE2, ">ludo.txt"); foreach my $paramKey (@params){ while(<FILE1>){ if (/$paramKey.*$/){ print FILE2, "\n"; } } close(FILE1); close(FILE2);
    Edit: I put the open/close functions in the right place. So now it doesn't overwrite ludo.txt for every loop.
      It doesn't print anything in the file! Why?
        Because open (FILE2, ">ludo.txt"); overwrites ludo.txt every time through the $paramKey loop.

        --

        flounder

Re: matching an array
by Skeeve (Parson) on Aug 08, 2003 at 12:12 UTC
    So you have:
    1. an array with your form values (@params)
    2. and a file to select lines from, depending on the values?
    don't you?

    If this is it, your approach is not so much wrong. You should consider swapping the loops. You won't need any indexing then.

    BTW: Is this homework?

      o you mean swapping the "foreach" loop and the "while" one? like this?
      open (FILE1, "<tabtext.txt"); open (FILE2, ">ludo.txt"); while(<FILE1>){ foreach my $paramKey (@params){ if (/$paramKey/){ print FILE2; } } } close(FILE1); close(FILE2);
      and it's not an homework!! LOL
Re: matching an array
by Zeroth (Beadle) on Aug 08, 2003 at 12:41 UTC
    I suggest you read perldoc perlre, or some good book about the subject, for learning more about regular expressions. You can do a lot of nifty stuff with regex's with really little effort.

    I really recommend the book: Mastering Regular Expressions

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-16 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found