Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Split Operation

by santhosh_89 (Scribe)
on Jul 30, 2009 at 04:53 UTC ( #784478=perlquestion: print w/replies, xml ) Need Help??
santhosh_89 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a scalar variable, which needs to be split into an array.

Input :

my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p";

Output :

@array = ( [a, b], c, 'd', [e, [f, g, h, [i, j], k, l], m, n], o, p);

Replies are listed 'Best First'.
Re: Split Operation
by duelafn (Vicar) on Jul 30, 2009 at 13:07 UTC

    This looks like a job for Text::Balanced!

    #!/usr/bin/perl use strict; use warnings; use Text::Balanced qw/extract_bracketed extract_multiple/; my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @split = grep { $_ ne ',' } extract_multiple( $test, [ \&extract_bracketed, qr/[^\[,]+/, ',', ] ); print "@split\n";

    Note: The default extract_bracketed will also extract (), {}, and <> delimited strings. Replace \&extract_bracketed with sub { extract_bracketed($_[0], '[]') } to restrict to [] delimited chunks.

    Good Day,

Re: Split Operation
by vinoth.ree (Monsignor) on Jul 30, 2009 at 05:41 UTC

    I just taken your input as array and done the below program

    use strict; use warnings; use Data::Dumper; my @test=qw( [ a b ] c d [ e [ f g h [ i j ] k l ] m n ] o p ); my ($append, $opencount, @temp, $closecount); foreach(@test){ if($_ eq '['){ ++$opencount; $append.=$_; next; } if($_ ne ']' && $opencount){ $append.=$_; print "----$append----\n"; next; } if($_ eq ']'){ --$opencount; $append.=$_; unless($opencount){ #print "------------=>$append<=---------------"; push @temp, $append; $append=undef;$closecount=0; $opencount=0; next; } next; } else{ #print "------------=>$_<=----------------"; push @temp, $_; } #print Dumper \@temp; } print Dumper \@temp;
Re: Split Operation
by nagalenoj (Friar) on Jul 30, 2009 at 12:52 UTC
    use strict; use warnings; use Data::Dumper; my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @array = split /,/,$test; my ($open, $current, @res); foreach (@array) { $current .= $_ if ($current or /\[/); $current .= ', ' if !(/\]/); $open++ if (/\[/); if (/\]/) { $open--; $current .= ', ' if ($open) ; } unless ($open) { if ( $current eq ', ') { push @res, $_; } else { push @res, $current; } undef $current; } } print Dumper \@res;
    This code gives you what you want. Hope you are new to post threads in perlmonks and from next time please provide your code and the problem you are facing. It will tell us the way you have proceeded and it gives more understanding on the question.
    I am not sure, whether this can be solved easily by using regex in split.
Re: Split Operation
by Marshall (Abbot) on Jul 30, 2009 at 19:23 UTC
    I was looking at this and thought, hey it can't be too hard...after some non-trivial effort, I whipped out Jeffrey Friedl's book, "Mastering Regular Expressions".

    In Chapter 5, page 194, he says: "on the vast majority of systems, you simply can't match arbitrarily nested constructs with regular expressions." There are ways in Perl to do some zoomy things like dynamically create a regex at some depth. But my investigation appears to indicate that this is NOT "just a split" and that figuring out that something like [e, [f, g, h, [i, j], k, l], m, n] is all one thing, is likely to be darn difficult in a single regex, if we assume that we just don't know how deep the nesting levels of [ ] go!

    There is a posting with some code that counts left and right brackets to figure out the depth. Do NOT assume that more lines is slower! Something is weird in my browser now and I can't look again at that code easily at the moment, but I suspect that it is likely to be (or something very similar to it) to be the fastest thing going. Either that or this "balanced" module that was also suggested.

    I'm just saying that if J. Friedl says this problem is hard, I'm unlikely at my skill level to arrive at a "simple" solution that proves him wrong.

    here is my effort at some code - I can't do it with one regex:

    #!/usr/bin/perl -w use strict; my $test="xyz,[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @test = $test =~ m/,|[\w']+|[\[\]]/g; my @result; while (@test) { my $t = shift @test; if ($t =~ /[\w']+/) { push (@result, $t); } #simple "word" thing elsif ($t =~ /\[/) #complex bracketed expression { push (@result, bracket_exp()); } # commas are skipped.. } sub bracket_exp { my $temp ="["; my $depth = 1; while ($depth) { my $next_thing = shift @test; $temp .= $next_thing; $depth++ if $next_thing eq '['; $depth-- if $next_thing eq ']'; } return ($temp); } foreach my $t ( @result) { print "$t\n"; } __END__ prints: xyz [a,b] c 'd' [e,[f,g,h,[i,j],k,l],m,n] o p
Re: Split Operation
by happy.barney (Pilgrim) on Jul 31, 2009 at 08:12 UTC
    my $test="xyz,[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @stack = []; for (split /([\[\],])/, $test) { next unless length $_; next if $_ eq ','; push @stack, [] and next if $_ eq '['; push @{ $stack[-2] }, pop @stack and next if $_ eq ']'; push @{ $stack[-1] }, $_; } my $retval = $stack[0];
      Dear Happy.Barney I tested your perl source code,You have splited the scalar value based on the open and close bracket.So it is failing one of the test case.I gave following input/output.
      #!/usr/bin/perl use strict; use warnings; my $test="[a[a]]"; my @stack = []; for (split /([\[\],])/, $test) { next unless length $_; next if $_ eq ','; push @stack, [] and next if $_ eq '['; push @{ $stack[-2] }, pop @stack and next if $_ eq ']'; push @{ $stack[-1] }, $_; } my $retval = $stack[0]; use Data::Dumper; print Dumper($retval); $VAR1 = [ [ 'a', [ 'a' ] ] ]; The output should be as following. $VAR1 = [ [ 'a[a]' ] ];
Re: Split Operation
by Anonymous Monk on Jul 30, 2009 at 05:09 UTC

      This is just regular expression. I don't need any conversion here. It needs just a split operation.

Re: Split Operation
by JadeNB (Chaplain) on Aug 03, 2009 at 05:16 UTC
    I was going to suggest
    my @array = eval '(' . $test . ')';
    just for amusement, but it doesn't work. I think that I will probably never understand string eval properly.
      This fails:
      my $test = "[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @array = eval ( $test );
      This succeeds:
      my $test = "[a,b,],c,'d',[e,[f,g,h,[i,j,],k,l,],m,n,],o,p"; my @array = eval ( $test );
      Don't forget no strict 'subs'; or else all those unquoted single characters will be treated as barewords!
        The only difference that I can see is that the second code has (apparently spurious) commas at the end of every arrayref—is that correct? Why is it necessary?

        Of course you're right that what I wrote is far from strict-safe—thanks! Making it so would probably be harder than just giving the kind of solution the OP wanted, though. :-)

        use strict; my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; $test =~ s/([a-zA-Z])/"$1"/g; my @array = eval ( $test );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://784478]
Approved by ikegami
Front-paged by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-06-24 21:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (562 votes). Check out past polls.