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

$ perl -MData::Dumper=Dumper -lwe '$p={}; $p->{qw(fee fie foe)} = (1.. +3); print Dumper $p' $VAR1 = { 'feefiefoe' => '' }; $ perl -MData::Dumper=Dumper -lwe '$p={}; @p->{qw(fee fie foe)} = ( 1..3); print Dumper $p' Can't coerce array into hash at -e line 1.
You can see what I'm trying to do, can't you? I want to assign an array to a slice of a refernced hash. But I'm damned if I can get the syntax right. I've tried changing quote mthods and diddling with curlies, ->s, $s and and @s until I'm blue in the face, but have had no luck. May I crave the monks' solicitude for my redemption?

Replies are listed 'Best First'.
Re: Syntax for slice of a referenced hash
by arturo (Vicar) on Apr 03, 2001 at 00:14 UTC

    Work with the hash more directly:

    use Data::Dumper; my $p = {}; @$p{qw(fee fie foe)} = 1 ..3; print Dumper($p);

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(tye)Re: Syntax for slice of a referenced hash
by tye (Sage) on Apr 03, 2001 at 02:04 UTC

    Others have answered your specific question. I found that dealing with references could get pretty confusing until I realized that there are only four rules and each is quite simple.

            - tye (but my friends call me "Tye")
Re: Syntax for slice of a referenced hash
by bbfu (Curate) on Apr 03, 2001 at 00:15 UTC

    Is this what you want?

    use Data::Dumper; @{$p}{qw(One Two Three)} = (1..3); print Dumper($p);

    Prints:

    $VAR1 = { 'Three' => 3, 'Two' => 2, 'One' => 1 };

    Update: Blast you, arturo, and your quick typing. ;-)

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: Syntax for slice of a referenced hash
by suaveant (Parson) on Apr 03, 2001 at 00:17 UTC
    Change $p->{qw(fee fie foe)} to @$p{qw(fee fie foe)}
    Then it'll work


                    - Ant

Re: Syntax for slice of a referenced hash
by domo (Initiate) on Apr 04, 2001 at 10:36 UTC
    Slightly belated thanks to those who replied. I'll be adding those rules to my rosary.