in reply to What is the most efficient way to split a long string (see body for details/constraints)?

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.
  • Comment on Re: What is the most efficient way to split a long string (see body for details/constraints)?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: What is the most efficient way to split a long string (see body for details/constraints)?
by Anonymous Monk on Jun 21, 2019 at 18:41 UTC
    I appreciate your answer, from a convenience perspective when speed is not an issue. i was not aware of the ease with which Text::CSV_XS can handle field extract. And, with the _XS part maybe it is performant as well?!