in reply to Re: searching data lines between keywords
in thread searching data lines between keywords

Where did keyword2 go? Perhaps this was a design decision. If not, adding the line @{$hash{$currentkey}} = (); into the if( $inkey == 0 ) block does the trick.

P.S.: In order to make Data::Dumper print very nice output, pass it a hash reference, as in Dumper(\%hash). Then the output is as follows:

$VAR1 = { 'mykeyword3' => [ 'baz3', 'foo3', 'bar3' ], 'mykeyword2' => [], 'mykeyword1' => [ 'foo1', 'bar1' ], 'mykeyword4' => [ 'baz4' ] };

P.P.S.: Can some enlightened monk tell me the preferred way to "touch" an array (reference). That is, if I only want to clear an array if it doesn't already exist (see my @{$hash{$currentkey}} = (); addition above). The snippet push @{$hash{$currentkey}}; works but produces a Useless use of push with no values warning.

Replies are listed 'Best First'.
Re^3: searching data lines between keywords
by jhourcle (Prior) on Jun 14, 2005 at 16:32 UTC
    P.P.S.: Can some enlightened monk tell me the preferred way to "touch" an array (reference). That is, if I only want to clear an array if it doesn't already exist (see my @{$hash{$currentkey}} = (); addition above). The snippet push @{$hash{$currentkey}}; works but produces a Useless use of push with no values warning.

    I typically use:

    $hash{$currentkey} ||= [];

    Which will set it to an empty array ref, if it isn't already a 'true' value, and undefined isn't true ... of course, there's lots of other not true values, as well (empty string, 0, etc.)

Re^3: searching data lines between keywords
by lupey (Monk) on Jun 14, 2005 at 16:50 UTC
    @{$hash{$currentkey}} is an array so you can truncate the array just as you would any other array. You don't need to use push. Both of these will do the trick

    @{$hash{$currentkey}} = (); $#{$hash{$currentkey}} = -1;

    Lupey

      I would like to create the array if it doesn't exist and not do anything otherwise. This is similar to the touch command in Unix-like environments. The command push @{$ref}; does exactly that: it pushes nothing onto the array, which creates it if necessary but doesn't change it if it exists.