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

Re: split with a delimiter, every 4 time it occurs.

by hdb (Prior)
on Jul 01, 2013 at 07:10 UTC ( #1041767=note: print w/replies, xml ) Need Help??


in reply to split with a delimiter, every 4 time it occurs.

Splitting can be done in other ways than using split. Here it is by far easier to describe the pieces that should result from the splitting and use a regex (a simple regex, relatively simple regex):

use strict; use warnings; my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N +','O','P','Q','R','S','T','U','V','W','X','Y','Z'"; my @pieces = $to_split =~ /('\w'(?:,'\w'){0,4})/g; $"="\n"; print "@pieces\n";

Or, if you insist on split, use the above as a delimiter, and remove the commas:

use strict; use warnings; my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N +','O','P','Q','R','S','T','U','V','W','X','Y','Z'"; my @pieces = grep {$_} split /('\w'(?:,'\w'){0,4}),?/, $to_split; $"="\n"; print "@pieces\n";

Replies are listed 'Best First'.
Re^2: split with a delimiter, every 4 time it occurs.
by Anonymous Monk on Jul 01, 2013 at 08:19 UTC
    I want this ','(not just comma) as the delimiter. I tried changein the coma to ',' but not working!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041767]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2017-06-23 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (546 votes). Check out past polls.