I have a line:
$string = \'Paul,12,\"soccer,baseball,hockey\",white\';
I am try to split this into @array that has 4 values so
<
try this
@array=($string =~ /^([^,]*)[,]([^,]*)[,]["]([^"]*)["][,]([^']*)$/);
the array will contains the output which expected by you.
$string = "Paul,12,\"soccer,baseball,hockey\",white";
1 while($string =~ s#"(.?),(.?)"#\"$1aaa$2\"#g);
@array = map {$_ =~ s/aaa/ /g; $_ =~ s/\"//g; $_} split(/,/, $string);
$" = "\n";
print "$array[2]";
In response to how to do it with Text::CSV(_PP). Here is a quick one.
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_PP;
my $parser = Text::CSV_PP->new();
my $string = "Paul,12,\"soccer,baseball,hockey\",white";
$parser->parse($string);
my @fields = $parser->fields();
print "$_\n" for @fields;
Normally one would install Text::CSV
or Text::CSV_PP
through the cpan
utility.
To work around your not being able to install modules, I suggest you use the 'pure Perl' implementation so that you can 'install' it. The above example would work assuming you copied the text of Text::CSV_PP source into a file named CSV_PP.pm
in a folder called Text
created in the same directory as your script. You could also put it in some other location and use the use lib 'directory'
method as discussed previously. See here and here to see other ways to get around install restriction using CPAN modules.
The standard module Text::ParseWords will do this as well.
my @array = parse_line(q{,}, 0, $string);
use strict;
use warning;
#use Data::Dumper;
my $string = qq/Paul,12,"soccer,baseball,hockey",white/;
#split string into three parts
my ($st1, $st2, $st3) = split(/,"|",/, $string);
#output: st1:Paul,12 st2:soccer,baseball,hockey st3:white
#split $st1 into two parts
my ($st4, $st5) = split(/,/,$st1);
#push records into array
push (my @test,$st4, $st5,$st2, $st3 ) ;
#print Dumper \@test;
print "$test[2]\n";
output:
soccer,baseball,hockey
#$VAR1 = [
# 'Paul',
# '12',
# 'soccer,baseball,hockey',
# 'white'
# ];
Use this regex: m/("[^"]+"|[^,]+)(?:,\s*)?/g;
The above regular expression globally matches any word that starts with a comma or a quote and then matches the remaining word/words based on the starting character (comma or quote).
Here is a sample code and the corresponding output.
my $string = "Word1, Word2, \"Commas, inbetween\", Word3, \"Word4Quoted\", \"Again, commas, inbetween\"";
my @arglist = $string =~ m/("[^"]+"|[^,]+)(?:,\s*)?/g;
map { print $_ , "\n"} @arglist;
Here is the output:
Word1
Word2
"Commas, inbetween"
Word3
"Word4Quoted"
"Again, commas, inbetween"