D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] PHP makes things easy



Simon Waters wrote:

> > $numerical = array_flip($alphabet);
> > ksort($numerical);
> 
> I think you are using too many associative arrays, but I could be wrong.

Me too.

> A common approach to sorting, is to skip it and just return an index of
> the values.
> 
> i.e. All we need in this circumstance is an array (non associative)
> myindex with values "3","1","0",2" so we can refer to
> alphabet[myindex[0]] and get the first value, alphabet[myindex[1]] to
> get the second. We don't need any hashes as provided by associative
> arrays as far as I can see?
> 
> #!/usr/local/bin/perl
> my @alphabet = ( "562389856", "123521623" , "898234234", "002342444" );
> print "@alphabet \n" ;
> my @myindex = sort { $alphabet[$a] <=> $alphabet[$b] } 0 .. $#alphabet ;
> print "@myindex \n" ;
> print "@alphabet[@myindex]\n";
> 
> 
> Whether perl actually implements this efficiently underneath is another
> question entirely.
> 
> I plead I'm a total Perl newbie, but I think that there is no need for
> associative arrays here, and that for large amounts of data it is better
> not to use them (any comp. sci. graduates going to explain if I'm wrong
> ?). All bets are off if there are large numbers of duplicates, and I
> know nothing of PHP.
> 
> The Perl documents "Far more than you've ever wanted to know about"
> explain this very well in sort.html, although I've no idea if I
> understood all of it.
> 
> > Can Perl/Python/Java/etc. do the same job in less code???
> > :-)
> 
> I dare say some perl line noise would implement array_flip using "map",
> or some bizarre perl array notation in one line, however I don't
> subscribe to the view that brevity of code is always the deciding factor
> ;-) We could always make it a subroutine, call it "flip" and do it in 6
> characters less ;-)
> 
> I better be careful as the gentleman who taught me all about algorithmns
> is probably reading and despairing about how much I've forgotten.
> 
>  Simon, whose knowledge on sorting and indexing is rank.
> -----BEGIN PGP SIGNATURE-----
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQE/YbfoGFXfHI9FVgYRAkWOAJ9EO1V3MTVD50JhZwZyptfNBf8xmACdG6mh
> Kf2lO4i+2DEJss7Z6Jo57v8=
> =Oya+
> -----END PGP SIGNATURE-----
> 
> --
> The Mailing List for the Devon & Cornwall LUG
> Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
> message body to unsubscribe.
> 

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly