Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Need to get multiple values.

by frank_2k4 (Acolyte)
on Dec 20, 2005 at 18:39 UTC ( [id://518146]=perlquestion: print w/replies, xml ) Need Help??

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

Update: My apologies for the confusion. The values have no space around the "=" which is why I am splitting it up by \s+. Also. Just to provide a little more clarification. I am trying to associate like values. So john.doe in this particular case correlates with john.doey. I need to associate these, and print them on one line. Where I need help is, I also need to extract doe, and doey, and print them on the same line. My criteria for matching values is:
/^john.doe/m /^john.doey/m /^doe/m /^doey/m
I need to explicitly associate john.doe with john.doey. If I use doe, and doey as my search strings, I run into issues with associating them correctly. So the suffix is common in all of the values I am looking for, however the prefix is different. Please let me know if I need to clarify further. Thank you so much for your help! Greetings Monks. Here is a little background. I am using LWP to grab a web page. The web page looks something like:
john.doe=18192812 tim=lasdflkdsajf john.doey=jr-1234 doe=12912039 bob=sdfasfd doey=jr-1235
So far I have split up the content and tossed it into an array:
@data = split /\s+/, $html_page;
This now gives me
@data = ("john.doe = 18192812", "tim = lasdflkdsajf", "john.doey = jr- +1234", "doe = 12912039", "bob = sdfasfd", "doey = jr-1235")
I need to grab the values from john.doe, and john.doey. I also need to grab the values from doe and doey. The goal is to correlate them and print them on one line as such.
print "$johndoevalue $johndoeyvalue\n"; print "$doevalue and $doeyvalue\n"
I am trying to figure out what the most efficient way to extract this is. Any help is mucho appreciado. Thanks!

Replies are listed 'Best First'.
Re: Need to get multiple values.
by davido (Cardinal) on Dec 20, 2005 at 19:01 UTC

    How could your code possibly achieve the example array you've shown? If you split "john.doe = 18192812" on whitespace, you will get this: @data = ( 'john.doe', '=', '18192812' ), not @data = ('john.doe = 18192812'). In other words, the results you're describing aren't possible given the raw data you've described, and the code you've shown.

    You could populate a hash like this:

    my %data; while( $html_page =~ m/([\w.]+)\s*=\s*([\w.-]+)/g ) { $data{$1} = $2; } print "$_ $data{$_}\n" foreach keys %data;

    Or if maintaining order is important, you could do it this way:

    my @data; while( $html_page =~ m/([\w.]+)\s*=\s*([\w.-]+)/g ) { push @data, [ $1, $2 ]; } print $data[$_]->[0], ' ', $data[$_]->[1], "\n" foreach 0 .. $#data;

    Dave

Re: Need to get multiple values.
by Roy Johnson (Monsignor) on Dec 20, 2005 at 18:52 UTC
    This looks like it would split very handily like
    my %contents = split /[\s=]+/, $html_page; print "@contents{'john.doe', 'john.doey'}\n";

    Caution: Contents may have been coded under pressure.
Re: Need to get multiple values.
by meraxes (Friar) on Dec 20, 2005 at 18:54 UTC

    This seems a little on the vague side. What is your criteria for matching values? When you match up the keys, will the secondary key always be the first key with a 'y' added to the end? If so, will the one with the y appended to it always be after the y-less one? Is there other matching criteria? Do you want the output to be joined with a space or with ' and '?

    If you clear up what your looking for I'm sure someone can help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found