DarshanS has asked for the wisdom of the Perl Monks concerning the following question:

I have an array Say, @array1 = qw (1,1,1,2,3,4,4,4,5,6,1,1,1). From this array i have to extract an output as "1,1,1,2,3,4,4,4,5,6" (i.e. to remove the number of 1's from the end alone). Infact i tried different way as suggested for similar questions, but couldn't get to it. I am NEW to perl, please do help me out. My Sincere apologies,updating with precise INFO: ----------------------------------------------- Series can be in any order. Not necessarily ascending, can be any random number in-between the 1's. Between the value '5' & '6', as demonstrated below, there will not be any 1's either. Say:
my @array1 = (1,1,1,5,3,4,4,4,2,6,1,1,1,1,1);
Expected output:(1,1,1,5,3,4,4,4,2,6) or (1,1,1,5,3,4,4,4,2,6,1) : getting anyone of it is fine.

Replies are listed 'Best First'.
Re: Read And modify a series
by AnomalousMonk (Archbishop) on May 26, 2015 at 11:52 UTC

    Per OP Update: I have an array Say, @array1 = qw (1,1,1,2,3,4,4,4,5,6,1,1,1,1,...). From this array i have to extract an output as "1,1,1,2,3,4,4,4,5,6,1" (i.e. to remove the X-number of 1's with just 1).

    One way:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = (1, 1, 1, 2, 3, 4, 4, 4, 5, 6, 1, 1, 1); dd \@ra; ;; my $one; $one = pop @ra while $ra[-1] == 1; push @ra, $one if defined $one; dd \@ra; " [1, 1, 1 .. 4, 4, 4, 5, 6, 1, 1, 1] [1, 1, 1 .. 4, 4, 4, 5, 6, 1]

    Update: Another way:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = (1, 1, 1, 2, 3, 4, 4, 4, 5, 6, 1, 1, 1); dd \@ra; ;; my $highest; for my $i (reverse 0 .. $#ra) { last if $ra[$i] != 1; $highest = $i; } $#ra = $highest if defined $highest; dd \@ra; " [1, 1, 1 .. 4, 4, 4, 5, 6, 1, 1, 1] [1, 1, 1 .. 4, 4, 4, 5, 6, 1]

    Upon reflection, I think the second approach is better: it's more flexible. If one wanted to trim all but one undef value from the end of an array, one need only change the test within the for-loop from
        last if $ra[$i] != 1;
    to
        last if defined $ra[$i];
    and the job's done. The first approach does not have this flexibility.


    Give a man a fish:  <%-(-(-(-<

      My Sincere apologies, Series can be in any order. Not necessarily ascending, can be any random number in-between the 1's. Between the value '5' & '6', as demonstrated below, there will not be any 1's either. Say: my @array1 = (1,1,1,5,3,4,4,4,2,6,1,1,1,1,1); Expected output:(1,1,1,5,3,4,4,4,2,6) or (1,1,1,5,3,4,4,4,2,6,1) : getting anyone of it is fine.

        You've been given solutions that will give you either of these outputs. Take your pick.


        Give a man a fish:  <%-(-(-(-<

Re: Read And modify a series
by Anonymous Monk on May 26, 2015 at 10:22 UTC
    I have an array Say, #array1 = qw (1,1,1,2,3,4,4,4,5,6,1,1,1). From this array i have to extract an output as "1,1,1,2,3,4,4,4,5,6" (i.e. to remove the number of 1's from the end alone).

    But that's not the same thing as finding unique values...?

    If you have an array, say my @array1 = (1,1,1,2,3,4,4,4,5,6,1,1,1);, one way to remove the 1's from the end is like this: pop @array1 while $array1[-1]==1;, but that might be inefficient if you have a large number of 1's at the end of the array.

    For help with your code, please make it more readable by adding <code> tags.

      Dear AM, I have modified the query accordingly. Kindly do help me out. Yes, it has nothing to do with unique. Was trying a different step, kindly ignore the same.
Re: Read And modify a series
by Anonymous Monk on May 26, 2015 at 11:06 UTC
Re: Read And modify a series
by Anonymous Monk on May 26, 2015 at 11:06 UTC
Re: Read And modify a series
by GotToBTru (Prior) on May 26, 2015 at 14:48 UTC

    Why do you need the repeated 1's removed? If the last number repeated is something other than a 1, should it be pruned too? Some of the solutions you've been given could be easily modified to allow for that.

    Dum Spiro Spero
A reply falls below the community's threshold of quality. You may see it by logging in.