Sombrerero_loco has asked for the wisdom of the Perl Monks concerning the following question:

Hi there.!

Im seeking for your wisdom!

I need to know how i can push this calue $ServiceRef fomr this piece of code to an array of hash of hashes:
elsif (/<ServiceRef>(.+?)<\/ServiceRef>/) { $serviceref = $1; $index{"ID$counter"}{"sons"} = [$serviceref]; }
As you see im capturing a regex expresion into $serviceref and i want toload every matches has been found into the array into de hash of hashes $index{"ID$counter"}{"sons"}[here should be an array reference, isnt ?].

As i has wrote its seems doesnt running properly, maybe something its mistakes. I need to do that because i need to keep every matched result of the regex expression because "a father could have more than a son".

Thanks in advance!

Replies are listed 'Best First'.
Re: how can push value from regex to a array of hash of hashes
by ikegami (Patriarch) on Jan 07, 2009 at 14:21 UTC

    With an array, you'd do

    push @array, $value;

    So with an array ref, you do

    push @{ $array_ref }, $value;

    In this case, that means

    push @{ $index{"ID$counter"}{"sons"} }, $serviceref;

    What you currently have,

    $index{"ID$counter"}{"sons"} = [ $serviceref ];

    replaces the that currently exists at $index{"ID$counter"}{"sons"}.

    By the way, please wrap code in <c>...</c> tags.

      Thanks ikegami. But this will push me the value obtained from the regex passes to $serviceref to an array under the hash %index{"ID$counter"}{"sons"} ?? I mean, sons will contain all the matches in an array ?
        yes