Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Zero-width assertions fail with split

by sk (Curate)
on Sep 19, 2005 at 06:50 UTC ( [id://493091]=note: print w/replies, xml ) Need Help??


in reply to Zero-width assertions fail with split

I think your -----0----- led to everyone suggest just split on , but i think you had -----,----- in mind.

Here is my take at this but does not do it in one split and not sure if it is even possible!

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $token = '-----'; my $data = "1,2,${token}0${token},4,5"; my $split = qr/(?<=$token),(?=$token)/; my @fields = split $split, $data; my @newfields = (); my $i = 0; while ($i < @fields/2) { my @f1 = split /,/,$fields[$i]; my @f2 = split /,/,$fields[$i+1]; my @dash = (pop(@f1), shift(@f2)); @newfields = (@newfields, @f1,join (',',@dash),@f2); $i += 2; } my @expected = ( 1,2, "${token}0${token}", 4, 5 ); print Dumper (\@newfields); print Dumper (\@expected); __END__ $VAR1 = [ '1', '2', '-----0-----', '4', '5,' ]; $VAR1 = [ 1, 2, '-----0-----', 4, 5 ];

if you pass in my $data   = "1,2,${token},${token},4,5";

Output

$VAR1 = [ '1', '2', '-----,-----', '4', '5' ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-20 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found