Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: hashes with arrays

by brian_d_foy (Abbot)
on Nov 07, 2007 at 02:19 UTC ( [id://649388]=note: print w/replies, xml ) Need Help??


in reply to hashes with arrays

Huh. Everyone wants to type a lot. :)

You're trying to create a "Hash of Arrays". Check out perldsc, the "Perl Data Structure Cookbook" for lots of examples. Here's how I created it from your example data:

#!/usr/bin/perl use strict; use warnings; my %hash = (); my $key; while( <DATA> ) { chomp; if( /^(\S.*)/ ) { $key = $1 } elsif( /^\s+(\S.*)/ ) { push @{ $hash{$key} }, $1 } elsif( /^\s*$/ ) { next; } } use Data::Dumper; print Dumper( \%hash ); __DATA__ Honda Civic Accord Toyota Camry Corolla Tundra Nissan Maxima
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found