On Mon, Feb 25, 2008 at 4:32 AM, Anirban Adhikary
<[EMAIL PROTECTED]> wrote:
> Dear List
>
> I need to delete each element after every execution of the foreach loop and
> I need to update & sorted the @arr1 after every execution of the foreach
> loop. I have tried a lot but not able to do this ............Pls
> help...........
>
> Thanks & Regards
> Anirban Adhikary
snip
You need to be careful with your language. Deleting an element after
a foreach loop would look like this
my @bar = sort { $foo{$a} cmp $foo{$b} } %foo;
for my $baz (@bar) {
}
delete $foo{$key};
@bar = sort { $foo{$a} cmp $foo{$b} } %foo;
I believe you are asking how to delete a key during a foreach loop:
my @bar = sort { $foo{$a} cmp $foo{$b} } %foo;
for my $baz (@bar) {
delete $foo{$baz};
@bar = sort { $foo{$a} cmp $foo{$b} } %foo;
}
You cannot do this; it breaks the foreach loop. It also makes no
sense to do it: the sort order hasn't changed because you have deleted
the an item, so why perform an expensive sort when you don't need to?
my @bar = sort { $foo{$a} cmp $foo{$b} } %foo;
for my $baz (@bar) {
delete $foo{$baz};
}
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/