On 11/2/05, Bryan R Harris <[EMAIL PROTECTED]> wrote: > > > I'd like to snag all the whitespace padded numbers in $_ into an array, e.g. > in the following: > > 1 "M_fx,-3,+2.p2" -31.4e-1 4. > > I'd like to pick up only the 1, -31.4e-1, and 4. > > I've tried: > > $ss = qr/(^|\s)[+-]?[0-9]+\.?[0-9]*([eE][+-]?[0-9]+\.?[0-9]*)?($|\s)/; > @list = m/$ss/g; > > ... but it slurps up the leading whitespace if it's there, and misses every > other number, amid other problems. I'd love to use \b, but it doesn't work > if there are "-" or "+" at the leading edge. > > Any good ideas on how to do this? > > - Bryan
Byran, As you've presented it, this is an incredibly tricky problem. split will help, though, because it will break everything down into smaller units: Now ask yourself if there are patterns you can look for in the elements. Will the 1st, 3rd, and 4th elements always be numbers? If so, then your're done, your numbers are $array[0], $array[2], and $arrary[3]. Otherwise, is there any restriction what kinds of numbers can appear in which positions? (e.g. can slot 0 have integers or floats, but not scientific notation?). Anything you can do to narrow down the possibilities will help. If a number of any type can appear in any location, then your only real option is to loop through, but you need to define a number for you purposes. Will your number ever start with a digit or "+" or "-"? Will a number ever contain a letter other than "e"? If not, you can try something like the following: my $string = qw(1 "M_fx,-3,+2.p2" -31.4e-1 4); my @array = split /\s/, $string; foreach my $element (@array) { if ( $element =~ /^[-+]?\d+\.?\d*(?:[eE]-?\d+)?/ ) { # it's a number; do something with it # warning: regex not tested } It might also be simpler, and possibly nearly as efficient, to break down the test into smaller test, performed sequentially. But try to appraoch the problem by breaking it--and the long string at the root of it--down into smaller bits. then deal with the bits. HTH -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!