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


In reply to Re: extracting from text by mwah
in thread extracting from text by heidi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.