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

Hi, Let me try this again.Data in the below grep always is getting saved to @Hash_filematches[0].For example if the below code greps for some "load.c",it saves the data in [0],next time it greps for someother file"common.c",it puts the data in the same [0].How do I make sure that data is not getting overwritten and data gets appended to[1] [2]......

%Hash_filematches=HASH(0x182f664) safety.plf = ARRAY(0xc5a1020) -----[0]= //
while () { ........ @{ $Hash_filematches{ $filename } } = grep( /\/\Q$file_name\E#/i, @{ + $Hash_filenames{ $filename } }); }

Replies are listed 'Best First'.
Re: How to append data in a hash
by bart (Canon) on Nov 07, 2010 at 17:56 UTC
    If you're talking about arrays as hash values, just use push.
    push @{ $Hash_filematches{ $filename } }, grep( /\/\Q$file_name\E#/i, +@{ + $Hash_filenames{ $filename } });

    That'll even work if $Hash_filematches{ $filename } doesn't exist yet, when this line is run, thanks to autovivification.

Re: How to append data in a hash
by kcott (Archbishop) on Nov 07, 2010 at 17:57 UTC

    What you want is push:

    push @array, $value;

    Looking at your code, ensure that $Hash_filematches{ $filename } is a reference to an array.

    Also note, it's not @Hash_filematches[0] - it's $Hash_filematches[0].

    -- Ken

      Thanks a lot,that what i want