On 27/01/2011 02:15, Greg J wrote:
I am trying to extract numeric values from a data file and I'm having
trouble writing a regular expression that would do this.

My data looks like this:<{5, 26}{20, 42, 64}{23, 48}>

I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
[23,48] ]

Any pointers would be greatly appreciated

Hi Greg

I am tempted to write

  my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
  my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];

But the program below seems to do what you described more clearly :)

HTH,

Rob


use strict;
use warnings;

my $data = '<{5, 26}{20, 42, 64}{23, 48}>';

my $list;

while ($data =~ /(\{.*?\})/g) {
  my @sublist = $1 =~ /\d+/g;
  push @$list, \@sublist;
}

use Data::Dumper;
print Dumper $list;

**OUTPUT**

$VAR1 = [
          [
            '5',
            '26'
          ],
          [
            '20',
            '42',
            '64'
          ],
          [
            '23',
            '48'
          ]
        ];

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to