Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Sorting numbers

by Anonymous Monk
on Jun 02, 2003 at 19:32 UTC ( [id://262466]=perlquestion: print w/replies, xml ) Need Help??

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

I have a text file with a number per line which I'm trying to sort and print numerically according to the number.

Text file:
0 1 6 4 7
My code is this:
@num[count2] = sort ($start_bit_no); $num[count2]++; print ("@num[count2];");

This prints out the order in which the numbers are sorted but my output is wrong:
My output should look like this:
1 2 4 3 5

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: Sorting numbers (not)
by tye (Sage) on Jun 02, 2003 at 20:25 UTC

    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
      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.
Re: Sorting numbers
by chromatic (Archbishop) on Jun 02, 2003 at 19:45 UTC

    Unless you're not showing a constant or a sub definition somewhere, your code shouldn't even compile under strict. That said, there are other problems.

    This line:

    @num[count2] = sort ($start_bit_no);

    sorts a one-element list, so you'll get back out exactly what you put in. I'm not sure where count2 comes from, but if it's not a constant or a sub (and I don't think it is, based on the next line), it'll be an undefined value.

    This line:

    $num[count2]++;

    increments the value you just assigned.

    You need to put the lines into an array or a list somehow (and don't forget to chomp them) and pass them to sort:

    print join("\n", sort map { chomp; $_ } <IN> );
Re: Sorting numbers
by Enlil (Parson) on Jun 02, 2003 at 19:47 UTC
    a quick look at 'perldoc -f sort' should clear things up (my guess is you are looking for a numerical sort not a string comparison sort).

    -enlil

Re: Sorting numbers
by TomDLux (Vicar) on Jun 02, 2003 at 19:48 UTC

    You're asking the wrong question .... while knowing how to sort by numbers would be usefully, you will learn a lot more by going to Tutorials and reading the articles there. Hint: start with Welcome to the Monastery.

    The answer you are looking for is available in: 2252.

    Or, on your machine, try: perldoc -f sort, or see perlfunc:sort if perldoc doesn't work for you. Similarly, you can find the man pages and FAQs at http://www.perldoc.com/.

Re: Sorting numbers
by diotalevi (Canon) on Jun 02, 2003 at 20:01 UTC

    Oops! Misread the problem. Here's something that works correctly

    $, = $/; $a[$_] = ++$line while <>; print map $a[$_] || (), $[ .. $#a;

    I goofed and wrote down the right answer to the wrong problem. Oops! This works if all of your numbers are positive.

    $, = $/; undef @a[<>]; print grep exists $a[$_], $[ .. $#a;

      Please don't use exists on array elements. It doesn't work on old versions of Perl and I hear that it will stop working again (thankfully) on some future version of Perl. You want defined instead. The difference between defined and exists makes sense for hash elements (tells you whether the key is present). The difference between defined and exists for array elements is an obscure memory allocation detail that you should never care about and can easily get wrong.

                      - tye
Re: Sorting numbers
by ehdonhon (Curate) on Jun 02, 2003 at 20:49 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://262466]
Approved by broquaint
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found