garymgordon
Programmer
I am looking at a Subroutine and short script that goes like this.
#!/usr/bin/perl -w
@array = (1,24,8,144,72,288);
foreach (sort sort_by_number(@array))
{
print ("$_ "
;
}
sub sort_by_number
{
if ($a < $b)
{
return -1;
}
elsif ($a == $b)
{
return 0;
}
else
{
return 1;
}
}
OUTPUT: 1 8 24 72 144 288
Now ....
The question is ...
I don't understand how the sort_by_number routine is performing its check on the @array??
I must be missing something in my thinking process.
When the subroutine runs, ... can you please help by explaining exactly the flow of what is being SORTED, how it is being sorted, and what is being assigned to $a and $b as the SORT PROCESS goes through.
I guess I don't understand how the SORT function is looking at the values contained in @array ... in order to sort the numbers and make any comparisons?
For example ... is the number 1 being compared to all the other numbers ... and if so, then what?
Please explain this process of how it works.
Thanks...
(PS .. the sooner you can reply to this question the better. I would greatly appreciate your help.)
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
#!/usr/bin/perl -w
@array = (1,24,8,144,72,288);
foreach (sort sort_by_number(@array))
{
print ("$_ "
}
sub sort_by_number
{
if ($a < $b)
{
return -1;
}
elsif ($a == $b)
{
return 0;
}
else
{
return 1;
}
}
OUTPUT: 1 8 24 72 144 288
Now ....
The question is ...
I don't understand how the sort_by_number routine is performing its check on the @array??
I must be missing something in my thinking process.
When the subroutine runs, ... can you please help by explaining exactly the flow of what is being SORTED, how it is being sorted, and what is being assigned to $a and $b as the SORT PROCESS goes through.
I guess I don't understand how the SORT function is looking at the values contained in @array ... in order to sort the numbers and make any comparisons?
For example ... is the number 1 being compared to all the other numbers ... and if so, then what?
Please explain this process of how it works.
Thanks...
(PS .. the sooner you can reply to this question the better. I would greatly appreciate your help.)
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer