Grouping perl foreach values

Steven Hanley sjh at
Tue Jul 9 11:41:09 EST 2002

On Tue, Jul 09, 2002 at 11:09:04AM +1000, Michael Still wrote:
> Hey,
> I've surfed and can't find anything on this, although that it probably
> because I don't know the perl word for it.
> I have a list of value in the form:
>   v1;v2;v3;v4;v5;v6;v7;v8;v9
> And I want to work through them in sets of three, something like:
> foreach($cola, $colb, $colc) ($listofthings){
>   ... do stuff ...
> }
> Can people either suggest how to do this, or suggest how I can refactor
> the problem to make this easier?
> Thanks,
> Mikal

well if the list is in a string/scalar lets put it into an array

my @arrayofthings = split /;/, $listofthings;
while (scalar (@arrayofthings)) {
   my ($cola, c$colb, $colc) = splice (@arrayofthings, 0, 3);
   # do whatever

will work (untested)

you may be able to do this with a regexp pulling the first three values off
the string each time through a loop also.

There are of course a few other ways to do it at least, this is perl after

	See You

sjh at
You are subtle as a window pane standing in my view
but I will wait for it to rain so that I can see you
   Anticipate - Ani

More information about the linux mailing list