On Sun, Dec 7, 2008 at 2:01 AM, Rob Dixon <[EMAIL PROTECTED]> wrote:
> Sharan Basappa wrote:
>>
>> I was wondering if there is a quick way to remove an arbitrary element
>> from an array.
>> I have an array which stores _ delimited strings a_b_c_1). The last
>> string stores the rank of the string.
>> I have to remove a string from array that has the lowest number.
>>
>> e.g. a_b_c_1, a_b_c_2. In this case, a_b_c_2 should be removed. The
>> strings are not arranged in any
>> specific order in the array.
>
> I'm hoping you've made a mistake, because if I understand you correctly then
> out
> of ('a_b_c_1', 'a_b_c_2') the first should be removed because 1 is less than
> two.
>
> If I'm right then the program below should be useful.
>
> HTH,
>
> Rob
>
>
>
> use strict;
> use warnings;
>
> my @data = qw/
> a_b_c_99
> a_b_c_6
> a_b_c_1
> a_b_c_2
> a_b_c_22
> /;
>
> my ($idx, $min_seq);
> foreach (0 .. $#data) {
> my ($seq) = $data[$_] =~ /(\d+)$/;
> next if defined $idx and $seq > $min_seq;
> ($idx, $min_seq) = ($_, $seq);
> }
>
> splice @data, $idx, 1;
>
You are right, the lowest ranked string should be knocked off.
I tried your code and it works perfectly. I have integrated that with
my real code.
Thanks a lot.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/