Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Having a problem creating a hash with the map function

by bbrelin (Initiate)
on Nov 29, 2012 at 16:52 UTC ( #1006302=perlquestion: print w/replies, xml ) Need Help??
bbrelin has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have a question regarding some perl code that i've written.

The code in question is looks like this:
#!/usr/bin/perl use strict; use Data::Dumper; my @dealers = ("Nissan","Toyota","Tesla","Chrysler","Ford","General Mo +tors"); my @cities = ("Belfast","Dublin","Cork","Derry","Tralee"); my %hashmap = (); my @car_city_dealer_aoa = ( [$cities[0],$dealers[1],$dealers[3],$dealers +[4]], [$cities[1],$dealers[2],$dealers[3],$dealers +[0]], [$cities[2],$dealers[0],$dealers[3]], [$cities[3],$dealers[1],$dealers[4],$dealers +[2]], [$cities[4],$dealers[2],$dealers[1],$dealers +[0],$dealers[5]]); # This is the goal #%Belfast_car_dealer_hash = ("Toyota"=> 1, # "Chrysler" => 1); # "Ford" => 1); %hashmap = map { my $arrayelem = $_; my $arraysize = scalar(@{$_}); $arrayelem->[$_] => 1 for (1..$arraysize-1) } $car_city_dealer_aoa[0]; print; print Dumper(%hashmap);
The problem that I have is that the map function doesn't actually return the list of key/value pairs. the output from Dumper is:
$VAR1 = ''; $VAR2 = undef;
I suspect it's something simple and silly, but I can't figure out what the map command is returning here. It should be returning a list like: Toyota => 1, Nissan => 1 Can anyone seen anything obviously wrong?


Braun Brelin

Replies are listed 'Best First'.
Re: Having a problem creating a hash with the map function
by tobyink (Abbot) on Nov 29, 2012 at 17:05 UTC

    Well, the contents of your map block indicate that you don't understand map much. It needs to return something (but cannot use the return keyword!). You have the for loop as the last statement in the block, which returns undef.

    # Try something like... # %hashmap = map { my ($city, @dealers) = @$_; $city => {map { $_ => 1 } @dealers}; } @car_city_dealer_aoa; print Dumper(\%hashmap);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Having a problem creating a hash with the map function
by choroba (Bishop) on Nov 29, 2012 at 17:13 UTC
    Others already told you how you could change the data structures. If you are still curious how to get the original hash, try this:
    my $skip_first; my %Belfast_car_dealer_hash = map { $skip_first++ ? ($_ => 1) : () } @{ $car_city_dealer_aoa[0] }; print Dumper \%Belfast_car_dealer_hash;
    I also noticed the script is missing warnings turned on.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Having a problem creating a hash with the map function
by Kenosis (Priest) on Nov 29, 2012 at 17:03 UTC
    # This is the goal #%Belfast_car_dealer_hash = ("Toyota"=> 1, # "Chrysler" => 1); # "Ford" => 1);

    The following will initialize your hash the way you want:

    my %Belfast_car_dealer_hash = map { $_ => 1 } @dealers[1,3,4];

    Dumper output of %Belfast_car_dealer_hash:

    $VAR1 = { 'Chrysler' => 1, 'Toyota' => 1, 'Ford' => 1 };

    Update: Added [1,3,4] to limit key creation.

    My apologies; I believe I've misunderstood. What about using a hash of arrays, where the key is the city and the value is a reference to an array of dealers? Perhaps this data structure would be helpful:

    use strict; use warnings; my @dealers = ( "Nissan", "Toyota", "Tesla", "Chrysler", "Ford", "Gene +ral Motors" ); my %citiesDealers = ( 'Belfast' => [ @dealers[ 1, 3, 4 ] ], 'Dublin' => [ @dealers[ 2, 3, 0 ] ], 'Cork' => [ @dealers[ 0, 3 ] ], 'Derry' => [ @dealers[ 1, 4, 2 ] ], 'Tralee' => [ @dealers[ 2, 1, 0, 5 ] ] ); print $_, "\n" for @{ $citiesDealers{'Belfast'} };


    Toyota Chrysler Ford
Re: Having a problem creating a hash with the map function
by space_monk (Chaplain) on Nov 29, 2012 at 17:05 UTC

    Perhaps a better question is why are you setting up the aoa in the first place, and what you're planning to do with your hashmap. I can't help but get the impression that <jedi>these aren't the data structures you're looking for </jedi>

    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Having a problem creating a hash with the map function
by NetWallah (Canon) on Nov 29, 2012 at 21:47 UTC
    If you wanted to get fancy and cryptic, you could create your "goal" hash by:
    my %Belfast_car_dealer_hash = map{$_=>1} @{$car_city_dealer_aoa[0]} [1..$#{$car_city_dealer_ao +a[0]}];
    but I would not write code like that without an explanation:
    It uses an array-slice of @{ $car_city_dealer_aoa[0] }, skipping the first (0) element, and mapping the rest.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006302]
Approved by tobyink
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-02-21 03:51 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.