Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Create a hash whose keys and values come from a given array

by tybalt89 (Monsignor)
on Sep 06, 2017 at 21:27 UTC ( [id://1198787]=note: print w/replies, xml ) Need Help??


in reply to Create a hash whose keys and values come from a given array

It's almost a one-liner :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1198743 use strict; use warnings; use Data::Dump 'pp'; my @array = ('a', 1, 2, 3, 4, 'b', 6, 7, 8); my $ref; my %hash = map { /\D/ ? ($_ => $ref = []) : (push @$ref, $_) x 0 } @ar +ray; pp \%hash;

Outputs:

{ a => [1 .. 4], b => [6, 7, 8] }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found