Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Zero-width assertions fail with split

by sk (Curate)
on Sep 19, 2005 at 06:50 UTC ( #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' ];


Comment on Re: Zero-width assertions fail with split
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-10-01 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (31 votes), past polls