Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to Split on specific occurrence of a comma

by LanX (Sage)
on Dec 03, 2020 at 21:27 UTC ( #11124601=note: print w/replies, xml ) Need Help??


in reply to How to Split on specific occurrence of a comma

Hi

instead of limiting split , I'd rather swallow all splitted cells into a long @array and then loop over that @array with splice to chunk out whatever length you want.

here a little demo in the debugger, looping over the alphabet in chunks of 10

DB<208> @array = a..z DB<209> while ( my @b = splice @array, 0, 10 ) { say "@b" } 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 DB<210>

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: How to Split on specific occurrence of a comma
by dirtdog (Monk) on Dec 03, 2020 at 22:07 UTC

    I think this approach should work. thanks!

      Your welcome! :)

      on a side note

      This

        $line =~ s/^[^=]*\=//;

      is IMHO better written as

        my ($var,$list) = split /=/, $line, 2;

      It's not only much easier to read and self-documenting, you'll also have a chance to check if $var eq "Teams"

      And if $var is of no interest you can also write

        my ( undef, $list ) = split /=/, $line;

      debugger demo

      DB<211> $line = "Teams=PATRIOTS,BILLS,DOLPHINS,JETS,COWBOYS,GIANTS,E +AGLES,REDSKINS,BENGALS,OILERS,STEELERS,BROWNS,SEAHAWKS,RAMS,49ERS,RAI +DERS" DB<212> x ($var,$list) = split /=/, $line 0 'Teams' 1 'PATRIOTS,BILLS,DOLPHINS,JETS,COWBOYS,GIANTS,EAGLES,REDSKINS,BENGAL +S,OILERS,STEELERS,BROWNS,SEAHAWKS,RAMS,49ERS,RAIDERS'

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      ) the 2 in case you are worried that the character "=" appears in your list elements

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-10-26 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?