#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    43
    Rep Power
    7

    Split every nth comma?


    Hi is there a way to split a string every nth comma and kill the comma that the split is on?

    for example I have a string 11111, 22222, 33333, 44444

    how can I split this into an array like:

    $array[0] = 11111, 22222

    $array[1] = 33333, 44444

    This is what I have but it doesn't work the way i want.
    Code:
    my ($tnr) = 11111, 22222, 33333, 44444;
    @chunk = split /.{14}/, $tnr;
    foreach (@chunk) {
      printf "%s\n", $_;
    Any help appreciated, thanks.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,258
    Rep Power
    1810
    No, the split function won't skip, but I'd look at your first statement.

    Code:
    my ($tnr) = 11111, 22222, 33333, 44444;
    What do you think $tnr contains?

    It only has the value 11111 in it.

    Perhaps you wanted

    Code:
    my $tnr = "11111, 22222, 33333, 44444";
    ?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    831
    Rep Power
    496
    I agree with Keath, the split function will not do what you want. You'll need to program something more elaborate to achieve your result.

IMN logo majestic logo threadwatch logo seochat tools logo