Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: A hash slice but not..

by smferris (Beadle)
on Feb 15, 2001 at 00:28 UTC ( [id://58442]=note: print w/replies, xml ) Need Help??

in reply to A hash slice but not..

Wouldn't you know it.. 10minutes after I send this, I remember eval! DOH! Following is what I've now come up with. Maybe someone has a better solution though.

#!/app/perl5.005/bin/perl use strict; use Data::Dumper; sub add2hash { my($hash,$value,@tree)=@_; my $tree='$hash->{'.join('}{',@tree)."}=$value"; eval $tree; } my(%tree); add2hash(\%tree,1,('a'..'c')); add2hash(\%tree,2,('a','b','d')); print Dumper(\%tree);

Shawn M Ferris
Oracle DBA

Replies are listed 'Best First'.
Re: Re: A hash slice but not..
by merlyn (Sage) on Feb 15, 2001 at 00:59 UTC
    No. We've gone through this already here before. And before, I also said that this eval solution fails because you end up compiling code repeatedly, and you could get very bad results if the data contains certain strings.

    Do not use eval-string. Ever. Or at least until you understand all the drawbacks.

    -- Randal L. Schwartz, Perl hacker

Re: Re: A hash slice but not..
by extremely (Priest) on Feb 15, 2001 at 00:40 UTC
    Change the my $tree='$hash->{'.join('}{',@tree)."}=$value"; line to my $tree='$hash->{'.join('}{',@tree).'}=$value'; or you will be REALLY unhappy the first time you send a string through... You don't want $value to be interpreted until the eval.

    Update: Nevermind, repeat after me kids, "eval is a four letter word". =)

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://58442]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-05-30 22:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.