Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Parsing data into an Array of Hashes

by kesterkester (Hermit)
on Sep 18, 2003 at 13:10 UTC ( #292378=note: print w/ replies, xml ) Need Help??


in reply to Parsing data into an Array of Hashes

This is a little clunky, and could use some whitespace-stripping, but it'll get across the general idea:

The while loop loops over your data, and the split breaks your line into a list of comma- and equal sign-separated items.

This list is then converted into an anonymous hash with the {} operator, and that hash is pushed onto the array @structure.

Note that this will work incorrectly if your values or field names have "="s or ","s in them.

#!perl use warnings; use strict; use Data::Dumper; my @structure; while (<DATA>) { chomp; push @structure, { split /,|=/ }; } print Dumper \@structure; __DATA__ Field1= a value,Field2= another value, Field3= yet another value Field1= a second value,Field2= another second value, Field3= yet anoth +er second value


Comment on Re: Parsing data into an Array of Hashes
Download Code
Replies are listed 'Oldest First'.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2015-07-07 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (91 votes), past polls