bcarroll has asked for the wisdom of the Perl Monks concerning the following question:
I am thinking I may need to use a different regular expression in the split statement, but wanted to check to see if anyone had a better approach.
Sample CSV:
Source,Destination,User,State 192.168.0.2,192.168.0.6,"cn=user1,ou=infrastructure,ou=accounts,o=ORG, +c=US",Allowed 192.168.0.3,192.168.0.6,"cn=user2,ou=infrastructure,ou=accounts,o=ORG, +c=US",Denied
This is what I have put together so far...
#!/usr/bin/perl use warnings; use strict; my $lineNum=1; print "<table>\n"; if ( -f $ARGV[0] ){ #$ARGV[0] is a file open(CSV,'<',$ARGV[0]); while (<CSV>){ csvLine2Html($_); } close(CSV); } else { #TODO... #$ARGV[0] is not a file } print "</table>\n"; sub csvLine2Html{ my $line = shift; chomp($line); if ($lineNum == 1){ #first line contains header information print "\t<tr>\n"; print map{ "\t\t<th>$_</th>\n" } split /,/, $line; print "\t</tr>\n"; } else { print "\t<tr>\n"; print map{ "\t\t<td>$_</td>\n" } split /,/, $line; print "\t</tr>\n"; } $lineNum++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split on comma unless within quotes...
by 2teez (Vicar) on Apr 08, 2014 at 19:21 UTC | |
|
Re: split on comma unless within quotes...
by frozenwithjoy (Priest) on Apr 08, 2014 at 18:45 UTC | |
|
Re: split on comma unless within quotes...
by Anonymous Monk on Apr 08, 2014 at 18:44 UTC | |
|
Re: split on comma unless within quotes...
by kcott (Archbishop) on Apr 08, 2014 at 18:51 UTC | |
|
Re: split on comma unless within quotes...
by Your Mother (Archbishop) on Apr 08, 2014 at 19:11 UTC | |
|
Re: split on comma unless within quotes...
by bcarroll (Pilgrim) on Apr 09, 2014 at 15:04 UTC | |
by Anonymous Monk on Apr 10, 2014 at 11:48 UTC | |
by LanX (Saint) on Apr 10, 2014 at 13:06 UTC |