G
Gerald Jones
Lets say I have an array with 10 elements, such as:
@a = ( "ian", "albert", "chris", "ed", "bertram", "david", "john", "fred",
"george", "harry");
I am interested in determining how many moves it would take to sort this
alphabetically. Now, I could re-implement Perl's built in sort mechanism and
have an incremented value in there somewhere, but first I'd like to know if
there was any way to do this with the built-in sort. The perldoc says this:
[snip]
If SUBNAME or BLOCK is omitted, sorts in standard string comparison order. If
SUBNAME is specified, it gives the name of a subroutine that returns an integer
less than, equal to, or greater than 0, depending on how the elements of the
list are to be ordered. (The <=> and cmp operators are extremely useful in such
routines.)
[/snip]
Huh? And how can I extrapolate these number is into number of moves to get to:
@a = ( albert", "bertram", "chris", "david", "ed", "fred",
"george", "harry", "ian", "john" );
..... anyways? So far, I got:
$ perl -e 'while (<>) { push( @a, $_) } @b = sort { print $a <=> $b, "\n" } @a; foreach $b (@b) { print "$b" }'
zed
ded
head
bread
aded
0
0
0
0
0
aded
bread
head
ded
zed
$
Which is wrong, but I have no idea how to interpret this let alone determine
how close I am to being correct. Any insight any one has would be appreciated!
Thanks, Gerald
@a = ( "ian", "albert", "chris", "ed", "bertram", "david", "john", "fred",
"george", "harry");
I am interested in determining how many moves it would take to sort this
alphabetically. Now, I could re-implement Perl's built in sort mechanism and
have an incremented value in there somewhere, but first I'd like to know if
there was any way to do this with the built-in sort. The perldoc says this:
[snip]
If SUBNAME or BLOCK is omitted, sorts in standard string comparison order. If
SUBNAME is specified, it gives the name of a subroutine that returns an integer
less than, equal to, or greater than 0, depending on how the elements of the
list are to be ordered. (The <=> and cmp operators are extremely useful in such
routines.)
[/snip]
Huh? And how can I extrapolate these number is into number of moves to get to:
@a = ( albert", "bertram", "chris", "david", "ed", "fred",
"george", "harry", "ian", "john" );
..... anyways? So far, I got:
$ perl -e 'while (<>) { push( @a, $_) } @b = sort { print $a <=> $b, "\n" } @a; foreach $b (@b) { print "$b" }'
zed
ded
head
bread
aded
0
0
0
0
0
aded
bread
head
ded
zed
$
Which is wrong, but I have no idea how to interpret this let alone determine
how close I am to being correct. Any insight any one has would be appreciated!
Thanks, Gerald