1

The data I need to parse looks like:

[fild1, filed2, .... filedn] , [filed1, filed2, .... filedn] .....

I call it a special form of CSV data because there are two kinds of comma:

  1. those commas outside the [] pair are served as the separator between different records.
  2. those commas inside the [] pair are served as the separator between different fields.

So in this case using split(',' , $data) will not serve my needs which is to parse the data and get every record in the data.

2
  • 2
    Who said anything about Python? Commented Dec 21, 2009 at 17:06
  • 2
    @carillonator, It's just standard Python vs Perl flame-baiting. Ignore it or flag it. Sadly, it's fairly typical on SO. Commented Dec 21, 2009 at 17:39

5 Answers 5

5

This should do the job:

my @out = map{[split/,/]} $data =~ /\[([^\]]+)\]/g;

example:

use Data::Dumper;
$data='[1,2,3],[4,5],[6]';
@a=map{[split/,/]} $data =~ /\[([^\]]+)\]/g;
print Dumper @a;

output:

$VAR1 = [
          '1',
          '2',
          '3'
        ];
$VAR2 = [
          '4',
          '5'
        ];
$VAR3 = [
          '6'
        ];
Sign up to request clarification or add additional context in comments.

Comments

2

How about: my @parts = split(/\]/, $data);, and then you can iterate over @parts, remove the heading [ and split once more by ","

You can also make the initial split like so: my @parts = split(/\] , /, $data); and that will save you some more cleanup later. Just be sure to only use this method if your data's whitespaces are consistent.

1 Comment

That's a bad regex for the split, since split's first argument is always interpreted as a regex.
1
my @a = split /\]\s*,\s*\[/, $data;

and get rid of first '[' and last ']'.

Comments

0

Here is a quick example that assumes that the value in $data is valid.

my @data = map { [ split ',', $_ ] } $data =~ / \[ ([^\[\]]*) \] ,? /xg;

Comments

0

you can also try out Text::CSV or Text::CSV_XS. go to CPAN to download.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.