Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Re: using map to generate a hash of hash

by jdporter (Paladin)
on Nov 07, 2002 at 14:56 UTC ( [id://211086]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: using map to generate a hash of hash
in thread using map to generate a hash of hash

Is each one of these records on a line by itself?
If so, then you want either to read the input line by line, or split the content on newlines first.

And then you probably want to use split rather than a regex, to parse each record.

Example:

my %hash = map { my( $a, @b ) = split /\|/; defined $a ? ( $a => { @b } ) : () } split /\n+/, $content;
Also, I wonder what the subhashes are supposed to look like. Do you have a fixed set of keys, and only the values come from the input records? In that case, you could have something like this:
my @subhash_keys = qw( something another ); my %hash = map { my( $a, @b ) = split /\|/; my %b; @b{ @subhash_keys } = @b; defined $a ? ( $a => \%b ) : () } split /\n+/, $content;
Of course, ultimately, you may find it more convenient to use a more traditional for loop. It would also be more efficient for large input files.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found