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

2001-03-03 Edit by Corion : Changed title, see first reply for real question.

  • Comment on How would I do an inplace edit, prepending '~foo' to every key of a hash? (was: How would I do an inplace edit, prepending)

Replies are listed 'Best First'.
Re: How would I do an inplace edit, prepending ~foo to every key of a hash?
by chipmunk (Parson) on Feb 09, 2001 at 03:10 UTC
    Here's a solution that uses map:
    use strict; my %hash = ( one => 'blah', two => 'echo', thr => 'aaaak', ); my %new_hash = map { ("~foo$_", $hash{$_}) } keys %hash;
      Beautiful. Simple. ++. Thanks!
Re: How would I do an inplace edit, prepending
by meonkeys (Chaplain) on Feb 09, 2001 at 03:06 UTC
    The complete question was:
    How would I do an inplace edit, prepending "~foo" to every key of a hash?

    So here's a kludge I came up with, but I'd like to do it with a map.
    use strict; my %hash = ( one => 'blah', two => 'echo', thr => 'aaaak', ); foreach my $key (keys %hash) { $hash{"~foo".$key} = $hash{$key}; delete $hash{$key}; } use Data::Dumper; print Dumper(\%hash);
        You know I ++ed that japhy and then decided that it was truly evil. OTOH at least it isn't in void context, eh? This helps the unclean feeling a bit:
        $hash{"~foo$_"} = delete $hash{$_} for keys %hash;

        --
        $you = new YOU;
        honk() if $you->love(perl)

Re: How would I do an inplace edit, prepending
by Fastolfe (Vicar) on Feb 09, 2001 at 03:11 UTC
    Update: Disregard -- a follow-up "answer" to this question provided more detail.

    Your question is rather vague, and has absolutely nothing to do with hashes. I suspect you might be looking for this:

    perl -pi.bak -e 'print "prepended\n" if $. == 1' file1 file2 ... filen
    Otherwise, consider posting your question in Seekers of Perl Wisdom and provide a bit more detail.
Re: How would I do an inplace edit, prepending
by coolmichael (Deacon) on Feb 10, 2001 at 11:45 UTC
    perl -pi.bak -e "print qq(this is a test\n) if ($.==1);" data.txt

    All I did was look at perldoc:perlrun. I seem to remember something else about prepending stuff to a file somewhere on perl monks. It can't be that easy can it?