Re: Puzzle: Know Your Data
- From: graeme_st_clair@xxxxxxxxxxxxxx (Graeme St.Clair)
- Date: Fri, 12 Aug 2005 16:31:44 -0400
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>
.
- References:
- Puzzle: Know Your Data
- From: Jeff 'japhy' Pinyan
- Re: Puzzle: Know Your Data
- From: Ugly Virgin
- Puzzle: Know Your Data
- Prev by Date: Re: Misuse of "use"?
- Next by Date: Re: Misuse of "use"?
- Previous by thread: Re: Puzzle: Know Your Data
- Next by thread: A simple perl script
- Index(es):
Relevant Pages
|
Loading