I'll answer my own question:
Inside optionsNextItem I'm making a copy of @{$ref}, so setting any
value in that copy won't change the array (stupid me).



-----Original Message-----
From: Duarte Cordeiro 
Sent: Monday, December 16, 2002 9:53 AM
To: '[EMAIL PROTECTED]'
Subject: array iteration & references


Hi all,

 I want to iteract through a array with a step = 2.
Meaning, that, with a array of (1,2,3,4,5,6,7,8,9,0) I want to have 5
interactions with 2elements available in each one. (1,2), (3,4), etc.

Not knowing if there is something OOTB within perl, I just:


sub optionsInitialize{
  my ($ref)=@_;
  Logit("ref is $ref, size is $#{$ref}");  
  return [$ref,0, $#{$ref}+1]+1;
}

sub optionsNextItem{
 my ($ref)=@_;
 my @ref_arr=@{$ref};
 my $count=$ref_arr[1];
 my $max=$ref_arr[2];
 my @arr=@{$ref_arr[0]};
 $count+=2;
 return undef if ($count>$max);
 $ref_arr[1]=$count;
 return ($arr[$count],$arr[$count+1]);
}

sub optionsIsEnd{
 my ($ref)=@_;
 my @ref_arr=@{$ref};
 my $count=$ref_arr[1];
 my $max=$ref_arr[2];
 return ($count >= $max);
}

And I would do something like:

  my $parameters=["par1", ["sjsj","djdj"], "par2", ["fjkdj","dkdj"];
  my $options=optionsInitialize($parameters);
  while(!optionsIsEnd($options)){
    ($key, $default)=optionsNextItem($options);
    ......
  }  

Now, the two questions:
1 - Is there a better (working) way to do this ?
2 - Thinking of other languages, I thought (my first error I know :P )
that passing a reference to a array would allow me to change the array
inside a sub. But $count is always 0 inside optionsNextItem();

Thanks in advance,

  Duarte Cordeiro

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to