Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sorting an associative array

Reply
Thread Tools

Sorting an associative array

 
 
Nathan Olson
Guest
Posts: n/a
 
      06-27-2004
I've got an associative array whose keys are movie titles. I'd like to sort
the keys in an order that takes into account titles beginning with "A" or
"The." In other words, "The Graduate" ought to be sorted with the 'g's, not
the 't's. Is there any way to do this that doesn't involve sorting manually?


Thanks in advance,
Nate Olson

 
Reply With Quote
 
 
 
 
BZ
Guest
Posts: n/a
 
      06-27-2004
Nathan Olson wrote in comp.lang.perl.misc:
> I've got an associative array whose keys are movie titles. I'd like to sort
> the keys in an order that takes into account titles beginning with "A" or
> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
> the 't's. Is there any way to do this that doesn't involve sorting manually?


Something like this should work:

sort {
$a =~ s/^(a|the)\s+//;
$b =~ s/^(a|the)\s+//;
$a <=> $b
} keys %hash;

--
BZ
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2004
Nathan Olson wrote:
> I've got an associative array whose keys are movie titles. I'd like
> to sort the keys in an order that takes into account titles
> beginning with "A" or "The." In other words, "The Graduate" ought
> to be sorted with the 'g's, not the 't's.


my @sortedtitles = sort {
($a =~ /(?i:the|a)?\s*(.+)/)[0]
cmp
($b =~ /(?i:the|a)?\s*(.+)/)[0]
} keys %movies;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-27-2004
Nathan Olson wrote:

> I've got an associative array whose keys are movie titles. I'd like to sort
> the keys in an order that takes into account titles beginning with "A" or
> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
> the 't's. Is there any way to do this that doesn't involve sorting manually


create a look up table (array) consisting of arrays with the first
element the title, and the second one the title with "A " and "The " etc
removed. Sort it on the *second* element.

Next use the first element to index your assoc array.

Google for 'Schwartzian Transform' for some nice examples.
for example: http://www.stonehenge.com/merlyn/UnixReview/col06.html

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-27-2004
BZ wrote:

> Nathan Olson wrote in comp.lang.perl.misc:
>
>> I've got an associative array whose keys are movie titles. I'd like to sort
>> the keys in an order that takes into account titles beginning with "A" or
>> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
>> the 't's. Is there any way to do this that doesn't involve sorting manually?

>
> Something like this should work:
>
> sort {
> $a =~ s/^(a|the)\s+//;
> $b =~ s/^(a|the)\s+//;
> $a <=> $b
> } keys %hash;


Which does O(n log n) replacements. It might be faster to create a
look-up table, with O(n) replacements, and use that to sort.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2004
Gunnar Hjalmarsson wrote:
>
> my @sortedtitles = sort {
> ($a =~ /(?i:the|a)?\s*(.+)/)[0]
> cmp
> ($b =~ /(?i:the|a)?\s*(.+)/)[0]
> } keys %movies;


Correction: Make that

my @sortedtitles = sort {
($a =~ /(??i:the|a)\s+)?(.+)/)[0]
cmp
($b =~ /(??i:the|a)\s+)?(.+)/)[0]
} keys %movies;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-27-2004


Gunnar Hjalmarsson wrote:

> Nathan Olson wrote:
>
>> I've got an associative array whose keys are movie titles. I'd like
>> to sort the keys in an order that takes into account titles
>> beginning with "A" or "The." In other words, "The Graduate" ought
>> to be sorted with the 'g's, not the 't's.

>
>
> my @sortedtitles = sort {
> ($a =~ /(?i:the|a)?\s*(.+)/)[0]


This fails (?) with Thesomething and Asomething. Yeah, I know that the
specs said A and The., but you missed the dot after The too, so that's
not an excuse

It also strips spaces in front of titles starting with spaces (ok there
probably are none like that)

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-27-2004
Gunnar Hjalmarsson wrote:

> Gunnar Hjalmarsson wrote:
>
>>
>> my @sortedtitles = sort {
>> ($a =~ /(?i:the|a)?\s*(.+)/)[0]
>> cmp
>> ($b =~ /(?i:the|a)?\s*(.+)/)[0]
>> } keys %movies;

>
> Correction: Make that
>
> my @sortedtitles = sort {
> ($a =~ /(??i:the|a)\s+)?(.+)/)[0]
> cmp
> ($b =~ /(??i:the|a)\s+)?(.+)/)[0]
> } keys %movies;


"Someone and the thingy"

(don't you need ^ ?)

And how to index %movies? I guess that the OP needs to access the info
in the %movies assoc.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2004
John Bokma wrote:
> Gunnar Hjalmarsson wrote:
>>
>> my @sortedtitles = sort {
>> ($a =~ /(??i:the|a)\s+)?(.+)/)[0]
>> cmp
>> ($b =~ /(??i:the|a)\s+)?(.+)/)[0]
>> } keys %movies;

>
> "Someone and the thingy"
>
> (don't you need ^ ?)


No. Unless a key starts with 'A ' or 'The ', (.+) captures the whole
key. (But ^ wouldn't have hurted, for the sake of clarity...)

> And how to index %movies? I guess that the OP needs to access the
> info in the %movies assoc.


Not sure what you mean. @sortedtitles can now be used to access the
info in %movies in the desired order.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2004
BZ wrote:
> Nathan Olson wrote in comp.lang.perl.misc:
>> I've got an associative array whose keys are movie titles. I'd
>> like to sort the keys in an order that takes into account titles
>> beginning with "A" or "The." In other words, "The Graduate" ought
>> to be sorted with the 'g's, not the 't's. Is there any way to do
>> this that doesn't involve sorting manually?

>
> Something like this should work:
>
> sort {
> $a =~ s/^(a|the)\s+//;
> $b =~ s/^(a|the)\s+//;
> $a <=> $b
> } keys %hash;


Did you try it?

- It does not replace case insensitively.
- It sorts strings numerically.

Besides that, since all the elements in the returned list are no
longer an (exact) key in the hash, how would you use the list?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Array and Hash (Associative array) in JavaScript v.3.0 VK Javascript 36 08-03-2005 02:39 PM
<FAQENTRY> Array and hash (associative array) VK Javascript 47 07-13-2005 06:27 AM
sorting an associative array keys based on values soup_or_power@yahoo.com Javascript 5 06-21-2005 01:05 AM
[newbie]saving and reading array of associative array Yvon Thoraval Ruby 5 09-17-2003 07:54 PM



Advertisments