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 | [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
|
|
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> );
| [reply] [d/l] [select] |
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 | [reply] |
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/.
| [reply] |
Re: Sorting numbers
by diotalevi (Canon) on Jun 02, 2003 at 20:01 UTC
|
$, = $/;
$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;
| [reply] [d/l] [select] |
|
| [reply] |
Re: Sorting numbers
by ehdonhon (Curate) on Jun 02, 2003 at 20:49 UTC
|
| [reply] |