On Sun, Dec 7, 2008 at 2:01 AM, Rob Dixon <rob.di...@gmx.com> 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;
>

Hi Rob,

I have a question on this. I realized that I also need to save the
element that I am removing from
the array. Would this code work (should remove the element and save it
in the variable)
$removed_element = splice @data, $idx, 1

Regards

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to