Edit: Bummer, this doesn't really work for you since that method can only be used on a whole file rather than separate lines.
I don't say it's the fastest way, but this is definitly the most flexible and convenient. This also sets you up for future changes in the field selection, you can even put the fragment-identifier into a config file or something.
use Modern::Perl;
use Text::CSV_XS;
use Data::Dumper;
my $csv = Text::CSV_XS->new({ sep_char=> "," });
#normally this would be a real file handle
my $line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
open my $fh, "<", \$line;
my $row = $csv->fragment($fh, "col=1;3;10-12;23-*");
print Dumper($row);
Output
$VAR1 = [
[
'a',
'c',
'j',
'k',
'l',
'x',
'y',
'z'
]
];
holli
You can lead your users to water, but alas, you cannot drown them.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.