in reply to Sorting numbers

So far everyone has sorted your numbers, which isn't what you asked for. Try this:

my @nums= <STDIN>; my @index= sort { $nums[$a-1] <=> $nums[$b-1] } 1..@nums; print join "\n", @index, '';
it prints:
1 2 4 3 5
just like you wanted.

                - tye

Replies are listed 'Best First'.
Re: Re: Sorting numbers (not)
by Anonymous Monk on Jun 02, 2003 at 21:02 UTC
    Thanks Tye for all your help :)
    I need to ask you another question, b/c what you typed does work but, my <STDIN> is from a text file which contains multiple strings seperated by commas and I'm reading the number which I've initialized to be $start_bit_no. When I replace <STDIN> with $start_bit_no. I don't get the results. Here is the code:
    foreach (@row1[$count1]) { @column1 = split(/\,/,@row1[$count1]); $sat_name = @column1[0]; $meas_name = @column1[1]; $parent_meas = @column1[2]; $start_bit_no = @column1[3];
    This is where $start_bit_no is initialized. Then I call a sub routine to sort what is in $start_bit_no. Which is the code you were first looking at. Why am I not seeing the output when I use your code?

    2003-06-02 edit ybiC: <code> tags

      You don't understand how sort works.

      You should know that sort expects a list (an array) to sort. Not just one element. When you call a subroutine to "sort everything in $start_bit_no", you will sort a scalar, i.e. JUST ONE NUMBER (or string).

      And you made a mistake in your assignments:

      $start_bit_no = @column1[3]; #don't use this when you mean $start_bit_no = $column1[3]; #this
      I suggest that you show us the whole piece and not just some parts to let us guess the rest.
        Here is the code:
        @column13 is an array that takes the data per column1, 3 position and stores it in $start_bit_no per line. It gets every number in the file in that position. Maybe I'm not doing this correctly as you stated. This is what my code looks like:
        open(STDIN1, "<icd_meas_child.txt") or die "Cannot open file"; @row1 = <STDIN1>; for (@row1[$done1]) { chop(@row1); foreach (@row1[$count1]) { @column1 = split(/\,/,@row1[$count1]); $sat_name = @column1[0]; $meas_name = @column1[1]; $parent_meas = @column1[2]; $start_bit_no = @column1[3]; print ("$sat_name;"); print ("$meas_name;"); &DESCRIPTION; &DATA; print ("$size_bits_no;"); &DATA1; print ("$monitor_type;"); print ("$parent_meas;"); &BIT; print ("$start_bit_no;"); &SUBSYSTEM; &BOX; print ("$lsb_proc_ind;"); print ("$blank"); print ("\n"); $count1++; } $done1++; } close STDIN1; sub BIT #routine to sort numbers { if ($meas_name = $parent_meas) { my @nums = $start_bit_no; my @index= sort { $nums[$a-1] <=> $nums[$b-1] } 1..@nums; #$index++; print join "\n","@index;",''; } }
        My text file is this:
        A1,BL1CHILDB0,BL1PARENTMON,0 A1,BL1CHILDB1,BL1PARENTMON,1 A1,BL1CHILDB2,BL1PARENTMON,2 A1,BL1CHILDB3,BL1PARENTMON,3 A1,BL1CHILDB4,BL1PARENTMON,4 A1,BL2CHILDB5,BL1PARENTMON,5 A1,BL2CHILDB6,BL1PARENTMON,6 A1,BL2CHILDB7,BL1PARENTMON,7 A1,BL2CHILDB3,BL2PARENTMON,1 A1,BL2CHILDB6,BL2PARENTMON,0 A1,D1CHILDB0,D1PARENTMON,0 A1,D2CHILDB1T3,D1PARENTMON,1 A1,D3CHILDB4T5,D1PARENTMON,4 A1,D4CHILDB6,D1PARENTMON,6 A1,D5CHILDB7,D1PARENTMON,7

        2003-06-03 edit ybiC: <code> tags