Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Multi Line Matching- getting into csv

by hdb (Prior)
on Aug 02, 2013 at 11:59 UTC ( #1047583=note: print w/replies, xml ) Need Help??

in reply to Multi Line Matching- getting into csv

Reading this kind of data is best done IMHO by first reading all of the data into an array of hashes (one hash per user) and then print in the desired format. This way you get nice and clean code with one line for each line of your data:

use strict; use warnings; my @data; while(<DATA>){ push @data, { 'user' => $1 } if /^user: (\w+)/; $data[-1]{'summary'} = $1 if /summary\s+"(.+)"/; $data[-1]{'accesslevel'} = $1 if /access-level\s+(\S+)/; $data[-1]{'group'} = $1 if /group\s+(\S+)/; } for my $user (@data){ print join ',', map { $user->{$_} // '' } qw(user summary acce +sslevel group); print "\n"; } __DATA__ user: myus44 [up] ------------ admin-state enabled summary "Johnny Cash" access-level group-defined group mi-group [up] user: jar1543 [up] ------------ admin-state enabled summary "Lara Croft" access-level group-defined group jar-head [up] user: myprivilegeduser [up] ----------- admin-state enabled access-level privileged

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047583]
[LanX]: hm just read an article on claiming that RPerl is 250 times faster. ... it's that even possible?
[marto]: under certain circumstances perhaps
[RonW]: as I understand it, RPerl "compiles" a subset of Perl to C, so I would expect the final executable to run a lot faster

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-05-22 20:13 GMT
Find Nodes?
    Voting Booth?