in reply to searching a file

If you have key => value pairs then a hash is the way to go:

my %hash; # get data as hash open FILE, $file or die $!\n; while (<FILE>) { my ($key, $value) = split; $hash{$key} = $value; } close FILE; my %changes = ( 'str1' => 'val1', 'str2' => 'val2' ); # change data in hash (or add new elements) $hash{$_} = $changes{$_} for keys %changes # overwrite old file with modified hash open FILE, ">$file" or die $!; print FILE "$_\t$hash{$_}\n" for keys %hash; close FILE;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: searching a file
by tmiklas (Hermit) on Mar 20, 2002 at 15:05 UTC
    Hi!

    Nice code - especially:

    $hash{$_} = $changes{$_} for keys %changes

    Greetz, Tom.
      If assigning changes in a hash I prefer to do the following
      @hash{keys %changes} = values %changes;
      I believe it does the same thing (unless I'm missing something) as values() returns the hash values in the same order as keys().
      TIMTOWTDI

      broquaint