Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Split Operation

by happy.barney (Friar)
on Jul 31, 2009 at 08:12 UTC ( [id://784836]=note: print w/replies, xml ) Need Help??


in reply to Split Operation

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];

Replies are listed 'Best First'.
Re^2: Split Operation
by santhosh_89 (Scribe) on Aug 03, 2009 at 05:53 UTC
    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]' ] ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://784836]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2026-01-15 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (118 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.