Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Indirect addressing problem

by 1nickt (Canon)
on Aug 28, 2015 at 02:01 UTC ( [id://1140274]=note: print w/replies, xml ) Need Help??


in reply to Indirect addressing problem

First, you shouldn't do that. See Why it's stupid to 'use a variable as a variable name'.

Second, why don't you have 'mailbox', 'subscribe' and 'country' as keys in a single hash? No need to have multiple hashes.

#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; my @values = ('inbox', 'yes', 'NZ'); my %data; $data{ $_ } = shift @values for qw/ mailbox subscribe country /; say Dumper \%data; __END__
Output:
[nick:~/monks]$ perl 1140269.pl $VAR1 = { 'subscribe' => 'yes', 'country' => 'NZ', 'mailbox' => 'inbox' };

Update: Or, if you have multiple records you can just put the code above in another loop. Below I make the hashref keys from a counter; perhaps you have user names or something that could be the keys.

#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my @records = ( [ 'inbox', 'yes', 'NZ' ], [ 'inbox', 'no', 'UK' ], [ 'outbox', 'yes', 'UK' ] ); my $href; my $index = 0; foreach my $record ( @records ) { ++$index; $href->{ $index }->{ $_ } = shift @{ $record } for qw/ mailbox subsc +ribe country /; } say Dumper $href; __END__
Output:
[nick:~/monks]$ perl 1140269.pl $VAR1 = { '1' => { 'country' => 'NZ', 'mailbox' => 'inbox', 'subscribe' => 'yes' }, '2' => { 'country' => 'UK', 'mailbox' => 'inbox', 'subscribe' => 'no' }, '3' => { 'country' => 'UK', 'mailbox' => 'outbox', 'subscribe' => 'yes' } };

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Indirect addressing problem
by tel2 (Pilgrim) on Sep 03, 2015 at 00:09 UTC
    Thanks very much for that, 1nickt.

    tel2

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found