Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how to match and assign?

by slok (Acolyte)
on Apr 24, 2001 at 13:27 UTC ( [id://74990]=perlquestion: print w/replies, xml ) Need Help??

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

how can I do the following... my problem is at the matching and assigning the variable....
I read in a line from a file.
The "line" is delimited by "|" and I need to compare
the string between the 4th and 5th "|" with the variable I have.
if it matches, I would then need to extract the string
between the 10th and 11th "|"

Replies are listed 'Best First'.
Re: how to match and assign?
by davorg (Chancellor) on Apr 24, 2001 at 13:53 UTC

    split is probably the easiest way to go here.

    my @values = split /\|/, $var; if ($values[4] eq $var) { # Values matched. # Use value in $values[10] } else { # values didn't match. }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      /me assumes that $/=undef is being applied, to allow all the file to be sucked into the variable $var.
      /me also assumes that $var is subsequently being reused to store the matching criteria?

      Or am I being dumb?

      --
      
      Brother Frankus.
        /me assumes that $/=undef is being applied, to allow all the file to be sucked into the variable $var.

        Er... no. The original poster mentioned line at a time processing. Assume the following code around my previous snippet:

        while (defined($var = <FILE>)) { # code here }
        /me also assumes that $var is subsequently being reused to store the matching criteria?

        Wasn't very clear exactly what the original poster wanted to do with the results, so I left it deliberately vague :)

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: how to match and assign?
by jeroenes (Priest) on Apr 24, 2001 at 14:22 UTC
    In an one-liner (just golf):
    @passed_items = map {split /\|/; $_[4] eq $var ? $_[10] : undef} <DATA +>;

    Jeroen
    "We are not alone"(FZ)
    Updated x 2 (ouch, that 'n' hurts!)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found