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

Re: extracting from text

by mwah (Hermit)
on Dec 10, 2007 at 10:44 UTC ( [id://656087]=note: print w/replies, xml ) Need Help??


in reply to extracting from text

I'd save an array of values under the hash key and sort afterwards (looks not very elegant but works):

... my @stuff= ( 'hi: 65 abcdefghijklmnopqrst 85', 'bye: 12 bcdefghijklmnopqrstu 32' +, 'hi: 86 sagfsdgsgwsehbbdgops 106', 'bye: 33 afasdfdfafasaafadfad 53 +'); my %cnt; /(\w+)\D+(\d+)\s+\w+\s+(\d+)/ && push @{$cnt{$1}},($2,$3) for @stuff; @$_ = sort { $a <=> $b } @$_ for values %cnt; print map "\$${_}_from=$cnt{$_}[0] and \$${_}_to=$cnt{$_}[-1]\n", keys %cnt; ...

displays here:

$hi_from=65 and $hi_to=106 $bye_from=12 and $bye_to=53

Addendum: after reading the other posts I'd think one could also beam the values into predefined my-Variables by an eval. Im not sure if (w/error handling added) this would be that bad:

use strict; # go for it use warnings; my @stuff= ( 'hi: 65 abcdefghijklmnopqrst 85', 'bye: 12 bcdefghijklmnopqrstu 32' +, 'hi: 86 sagfsdgsgwsehbbdgops 106', 'bye: 33 afasdfdfafasaafadfad 53 +'); my %cnt; /(\w+)\D+(\d+)\s+\w+\s+(\d+)/ && push @{$cnt{$1}},($2,$3) for @stuff; @$_ = sort { $a <=> $b } @$_ for values %cnt; my ($hi_from, $hi_to, $bye_from, $bye_to); # if we *know* what to exp +ect while( my ($k,$v) = each %cnt ) { my @evil_plan = ( "\$${k}_from=$$v[0]", "\$${k}_to=$$v[-1]" ); print join(' and ', @evil_plan), "\n"; # show what we prepared map eval, @evil_plan; # execute the unthinkable } print "\n$hi_from, $hi_to \n$bye_from, $bye_to\n"; # control

Regards

mwa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-26 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found