Re: Puzzle: Know Your Data



Hmm. I'm glad UV came up with h[er|is] solution, as mine was going to be similar, but involving the German double s that looks (sort of) like a Greek beta. Neither UV's solution nor mine showed the "feature" on Windows XP SP2 plus:-

This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 810 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun  1 2004 11:52:21

The arrays came out the same, and in mine the order depended on whether 'use locale' was included.

Looking forward to the answer, GStC.


----- Original Message ----- From: "Ugly Virgin" <perl@xxxxxxxxxxxxxx>
To: "Jeff 'japhy' Pinyan" <japhy@xxxxxxxxxxxx>
Cc: <beginners@xxxxxxxx>
Sent: Friday, August 12, 2005 11:53 AM
Subject: Re: Puzzle: Know Your Data



Jeff 'japhy' Pinyan wrote:
Here's an interesting exercise for beginners. You have an array of strings, and you want to sort them but in a case-insensitive manner. You are doing this in two different places in your code. In one place, you write:

  # convert the strings to lowercase when comparing
  my @sorted = sort { lc($a) cmp lc($b) } @data;

And then later in your code, forgetting how you'd done it before, you use:

  # convert the strings to uppercase when comparing
  my @ordered = sort { uc($a) cmp uc($b) } @data;

But this gives you a headache:  the arrays AREN'T IDENTICAL.

The puzzle to you is to determine the reason and sample data that demonstrates the problem.


one possible answer: symbols in the data (specifically the ones between the upper and lower case characters in ASCII)
$ perl -le'$,="\n";@a=("Hello", "_world!"); print sort {lc ($a) cmp lc ($b)}@a; print "\n", sort {uc($a) cmp uc ($b)}@a'


_world!
Hello


Hello _world!


-- To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx For additional commands, e-mail: beginners-help@xxxxxxxx <http://learn.perl.org/> <http://learn.perl.org/first-response>





.



Relevant Pages

  • Puzzle: Know Your Data
    ... You have an array of strings, and you want to sort them but in a case-insensitive manner. ... # convert the strings to lowercase when comparing ...
    (perl.beginners)
  • Re: Puzzle: Know Your Data
    ... strings, and you want to sort them but in a case-insensitive manner. ... # convert the strings to lowercase when comparing ...
    (perl.beginners)
  • Re: Solution for sorting an array alpha-numerically
    ... strings up into groups and sorting the groups seperately, ... > so that numeric and alphabetic data sort as seperate groups. ... To the same project as the web page, add the class AlphaNumCompare() ...
    (microsoft.public.dotnet.general)
  • Re: Error Testing
    ... "I've been Googling for websites explaining this sort of stats, ... using Fisher's Exact Test. ... data of this sort (binomial, comparing two fractions, etc.) has a long ...
    (sci.stat.edu)
  • Re: Error Testing
    ... "I've been Googling for websites explaining this sort of stats, ... using Fisher's Exact Test. ... data of this sort (binomial, comparing two fractions, etc.) has a long ...
    (sci.stat.edu)

Loading