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

Dear monks,

My problem is simply I have one array containing lots of numbers in ascending order. I also have a smaller array containg a few select numbers:

e.g. my @numbers = ('76.8', '77.0', '77.2' ,'77.4', '77.6', '77.8' , '78.0' + , '78.2', '78.4', '78.6' , '78.8', '79.0', '79.2', '79.4', '79.6', ' +79.8' ,'80.0' , '80.2' , '80.5' ,'80.7' ,'80.9',' '81.1'); my @small = ('78.6' , '78.8', '79.0', '80.9', '77.0');
The problem is that the numbers in @small should be in the same consecutive order as that of @numbers (e.g should contain: @small = ('78.6' , '78.8', '79.0', '79.2', '79.4'); ).

How can I devise a check that compares the order in @small to that of @numbers and changes any anomalies like in the array above ???(even if the first or last numbers are wrong). Many thanks,

Illogical monk

Replies are listed 'Best First'.
Re: Array logic!
by Abigail-II (Bishop) on Aug 06, 2003 at 12:33 UTC
    If even the first number of the small array could be wrong, how does one determine what the "right" small array is? Is
    @small = @numbers [0 .. $#small];

    a correct solution? If not, why not?

    I suggest you specify better how @small should look like - chances are you find a solution before posting the specificiation. And if not, you don't risk that a dozen people suggest answers, all doing something else than what you really want.

    Abigail

Re: Array logic!
by BrowserUk (Patriarch) on Aug 06, 2003 at 12:33 UTC
    #! perl -slw use strict; my @numbers = qw[ 76.8 77.0 77.2 77.4 77.6 77.8 78.0 78.2 78.4 78.6 78.8 79.0 79.2 79.4 79.6 79.8 80.0 80.2 80.5 80.7 80.9 81.1 ]; my @small = qw[ 78.6 78.8 79.0 80.9 77.0 ]; my $i=0; $i++ until $numbers[ $i ] == $small[ 0 ]; @small = @numbers[ $i .. ( $i + $#small ) ]; print "@small"; __END__ P:\test>test 78.6 78.8 79.0 79.2 79.4

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      That can't be right. It assumes that the first element of @small is correct, but it was explicitely mentioned that the first element could be wrong.

      Abigail

        Reading between the lines of the post and attemptng to interprete the intent from the example given, I took the "even if first and last are incorrect" to mean the lowest and highest values in the small array.

        This was the only interpretation I could make fit the data supplied.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Array logic!
by bobn (Chaplain) on Aug 06, 2003 at 13:06 UTC

    The statement of the problem is flawed. Since @numbers is sorted numerically, @small = sort {$a <=> $b } @small should be sufficient.

    Unfortunately, his statement of what @small should be doens't seem to match the problem statement, in that @small actually contains 80.9, but what he says @small should be does *not* contain 80.9 - what are we to make of that?

    Anyhow, if we assum that @small is a subset of @numbers, which the AM wants ordered the same way as @numbers, and for some reason, sort() is not the solution, the following would seem to work for any ordering for @numbers.

    my @numbers = qw(76.8 77.0 77.2 77.4 77.6 77.8 78.0 78.2 78.4 78.6 78.8 79.0 79.2 79.4 79.6 79.8 80.0 80.2 80.5 80.7 80.9 81.1); my @small = qw(78.6 78.8 79.0 80.9 77.0); $s{$_}++ for @small; @ordered_small = grep { exists $s{$_} } @numbers; # demo code below: print "@ordered_small\n"; @numbers = reverse @numbers; @ordered_small = grep { exists $s{$_} } @numbers; print "@ordered_small\n";

    Update: On closer reading, the problem statement is hopeless. How are the numbers in small "selected", and what makes the first or last (or any other) number in @small "wrong"?

    --Bob Niederman, http://bob-n.com
Re: Array logic!
by Abstraction (Friar) on Aug 06, 2003 at 12:24 UTC
    First things first you'll want to sort your arrays.

    Update: After re-reading the question and reading the responses of others it's unlear what the original poster is asking.

      Uhm, no, I don't think so. First, the big array is already sorted, that's a given, and so is the array in the example. Second, if you sort the second array, how would you which numbers of the smaller array are correct, and which arent'?

      Abigail