Re: Hash Sorting Problem
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Mon, 25 Feb 2008 07:49:48 -0500
On Mon, Feb 25, 2008 at 4:32 AM, Anirban Adhikary
<anirban.adhikary@xxxxxxxxx> wrote:
Dear Listsnip
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
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.
.
- References:
- Hash Sorting Problem
- From: Anirban Adhikary
- Re: Hash Sorting Problem
- From: John W. Krahn
- Re: Hash Sorting Problem
- From: Anirban Adhikary
- Hash Sorting Problem
- Prev by Date: Re: Hash Sorting Problem
- Next by Date: Re: scalar / hash problem in HTML::Parser
- Previous by thread: Re: Hash Sorting Problem
- Index(es):