Gossamer Forum
Home : General : Perl Programming :

Hash Slice

Quote Reply
Hash Slice
I've just had to use a hash slice for the first time ever...how exciting.

Code:
my @fields = $IN->param('fields');
my %slice;

@slice{@fields} = map { [ 'some', 'stuff' ] } @fields;

It's pretty cool and helpful....strange it's taken me so long to actually use one.

Last edited by:

Paul: Jun 21, 2002, 11:13 AM
Quote Reply
Re: [Paul] Hash Slice In reply to
Paul,

I have starred at this post for near 10 minutes now.... and I am no further forward in understanding it!Wink


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hash Slice In reply to
Wink

It's a neat little syntax trick.

The following two code snippets are equivalent.

Pretty basic:

Code:
%a = ( a => 'b', c => 'd' );
%b = ( e => 'f', g => 'h' );
$ populate b with values from a
foreach my $key ( keys %a ) {
$b{$key} = $a{$key};
}

And Paul's way:

Code:
%a = ( a => 'b', c => 'd' );
%b = ( e => 'f', g => 'h' );
$ populate b with values from a
@a{keys %b} = values %b;

And you've saved two lines of code! If you expect to redistribute your code, don't go crazy with it though, many people will have a hard time modifing all this perl xyzzy/job security stuff.
Quote Reply
Re: [Aki] Hash Slice In reply to
Nice explanation!

Quote:
many people will have a hard time modifing all this perl xyzzy/job security stuff


Not to mention myself, when I forget what it all means againWink


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Aki] Hash Slice In reply to
Code:
%a = ( a => 'b', c => 'd' );
%b = ( e => 'f', g => 'h' );
$ populate b with values from a

foreach my $key ( keys %a ) {
$b{$key} = $a{$key};
}

--------------------------------------------------------------------------------


And Paul's way:


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------


%a = ( a => 'b', c => 'd' );
%b = ( e => 'f', g => 'h' );
$ populate b with values from a

@a{keys %b} = values %b;

Don't you have the hash slice back to front there?....it looks like it should be:

@b{keys %a} = values %a;

Otherwise the foreach loop is creating:

$b{a} = 'b'
$b{c} = 'd'

...and the hashslice creates:

$a{e} = 'f'
$a{g} = 'h'

Last edited by:

Paul: Jun 22, 2002, 2:43 AM
Quote Reply
Re: [Paul] Hash Slice In reply to
Angelic Aoch, ya got me. You're right! Hopefully I'll still get brownie points for the effort? Blush
Quote Reply
Re: [Aki] Hash Slice In reply to
Definitely. You can have your "Helping the Community" badge too :)
Quote Reply
Re: [Aki] Hash Slice In reply to
I'm starting to find more and more uses for hashslices now...for example with my plugin system, when uploading files a user may wish to delete some by ticking checkboxes so I can just do:

Code:
delete @$input{ map { "file_$_" } @bad };

Its so much easier than a foreach loop or something like that.
Quote Reply
Re: [Paul] Hash Slice In reply to
hehe, another idea: once in awhile i throw into my code a

Code:
@$tags{ keys %$source } = values %$source;

but that's a bit confusing for code browsers in my opinion ;)

Last edited by:

Aki: Jul 23, 2002, 9:59 AM
Quote Reply
Re: [Aki] Hash Slice In reply to
>>
but that's a bit confusing for code browsers
<<

All the better :)