http://www.perlmonks.org?node_id=1047505

symgryph has asked for the wisdom of the Perl Monks concerning the following question:

I have some input like this:

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

I have written the following program which seems to regex correctly, but getting things to 'print' correctly seems to require more than the naieve 'if' logic I am applying. Was wondering if someone could take a peek and suggest something better.

#!/usr/bin/perl -w while (<>) { if (/^user:/) { chomp(); $uid=$_; $uid=~m/user:\s+(\S+)/; $uid=$1; #print "$uid,"; } if (/summary/) { chomp(); $summary=$_; $summary=~m/summary\s+\"(.+)\"/; $summary=$1; #print "$summary,"; } if (/access-level/) { chomp(); $accesslevel=$_; $accesslevel=~m/access-level\s+(\S+)/; $accesslevel=$1; #print "$accesslevel,"; } if (/group\s+/) { chomp (); $group=$_; $group=~m/group\s+(\S+)/; $group=$1; #print "$group"; } print "$uid,$summary,$accesslevel,$group\n"; }

My hoped format was something like:

myuser,Johnny Cash,group-defined,migroup myprivilegeduser,,privileged,,

With 'unused' things left as ,,'s.

"Two Wheels good, Four wheels bad."