David Kalnins writes ..

>I was wondering if it was possible to remove an item from a list whilst
>one is repeating over it with a foreach statement. For example, I was
>hoping that the following code fragment would do this - but instead it
>does exactly what I told it to do :) That is it leaves the item there
>but with an undefined value.

there's not really a way of doing it like you're doing because basically if
Perl removes the element then the next for iteration will skip the next
element

let me demonstrate .. one might be tempted to do this

  my @list = qw/alpha beta omega/;

  for( 0..$#list)
  {
    splice @list, $_, 1 if $list[$_] =~ /b/;
  }

and for the current example that will work .. but try it for this list

  qw/alpha beta bingo omega/;

and you'll see the problem .. the splice chops out that element - and the
next element slots down to take its place .. then the for loop goes to the
next element and tests it and misses the one that slotted down

also .. the end of the array is reached before the loop is finished (because
the array has shrunk) so when run under warnings - the above code generates
warnings

so what to do .. well instead of deleting items from the current list we
really need to create a new list which contains everything other than what
we don't want

wouldn't it be nice if Perl had some sort of function which iterated over a
list testing each element as it went and returned the resulting list

well .. I'm glad you asked .. enter the 'grep' function .. your code turns
into this

  my @list = qw/alpha beta bingo omega/;

  @list = grep { ! /b/ } @list;

  print join( "\n", @list);

and as you can see .. like all other assignments - we're able to use the
same container for the result that we're using for the input

and also - when outputting a list with a common separator between the
elements .. give 'join' a go in your print statement .. much neater than
another for statement

  perldoc -f grep
  perldoc -f join

-- 
  jason king

  A Canadian law states that citizens may not publicly remove bandages.
  - http://dumblaws.com/

Reply via email to