in reply to Parsing and Matching Text with Map

You're close.

push @employees_my_try, ( map { if( my($idx, $rest) = $_ =~ m/^employee\[(.*?)\] = new employee_ +element\((.*)\);$/ ) { my @split = map { s/\A"(.*)"\z/$1/; $_ } split( /,/, $rest ); [ $idx, @split ] } else { () } } split "\n", $data ); print Dumper \@employees_my_try;

Replies are listed 'Best First'.
Re^2: Parsing and Matching Text with Map
by awohld (Hermit) on Oct 05, 2006 at 19:19 UTC
    Okay, now I made a little change. I'm storing the data in a hash and the key is the index.
    # Create hash with employee index as key. my %employees = ( map { $_ =~ m/^employee\[(.*?)\] = new employee_element\((.*)\);/g } split "\n", $data );
    Right now the "value" is a string. How can I get it to change to an array inside this map expression?

    I can alter it outside the function using this:
    $employees{$_} = \[split ",", $employees{$_} ] foreach keys %employees;
      You process as advised, and at the end of the map block you return a key/value pair:
      map { ... $index, [ @list ]; }

      That will be fine to construct a hash.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}