[B]Perl sort quick question[/B]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [B]Perl sort quick question[/B]
# 1  
Old 10-09-2008
[B]Perl sort quick question[/B]

I've done a quick Google about this, but could not find the answer I want.

Say, there is an array like this:
@A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22);

I want to sort the array in the first order:
@sorted = qw(a1 a12 a2 b2 b22 b4 cd1 ef hij);

And second order:
@sorted = qw(a1 a2 a12 b2 b4 b22 cd1 ef hij);

Is there a way to do this in Perl without using modules?

Thanks a lot!

Last edited by zx1106; 10-09-2008 at 01:44 PM..
# 2  
Old 10-09-2008
Do you consider the sort function a module?
Code:
@sorted=sort(@A);

# 3  
Old 10-09-2008
Thanks! It works for the first order.
# 4  
Old 10-09-2008
What about the second order:
qw(a1 a2 a12 b2 b4 b22 cd1 ef hij);
Thanks!
# 5  
Old 10-09-2008
Yes, you can sort with a custom rule. But what exactly is the rule for the sort? If you cannot work out a formal specification of the sort, no one will be able to do it "correctly".

The following gives the same ordering as you mentioned, but there is no guarantee that it is going to give you the "correct" ordering (as you might expect) for other input values:

Code:
use Data::Dumper;

@A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22);
print Dumper([sort {
	my ($_a, $_b);
	for ([$a, \$_a], [$b, \$_b]) {
		$_->[0] =~ /^(.+?)(\d*)$/;
		${$_->[1]} = [$1, $2];
	}
	($$_a[0] cmp $$_b[0]) || ($$_a[1] <=> $$_b[1]);
} @A]);

# 6  
Old 10-10-2008
Second order which appears to be first sort by alpha then digit:

Code:
my @A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22);

my @sorted = map{$_->[2]}
          sort{$a->[0] cmp $b->[0] || $a->[1] <=> $b->[1]}
          map{/([a-z]*)(\d*)/;[$1,$2,$_]} @A;

print "$_\n" for @sorted;

Google "Schwartzian Transform" if interested in the cached key sort technique.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching - Quick Perl Question

Hi everyone I have a quick perl matching question. I have the following file, and I want to use perl to search through the 2nd column and see if it finds any of the month names (e.g.: Jan, Feb, Mar, ... Dec). Here's the file I'm trying to search, and here's the code I have so far. Any help... (5 Replies)
Discussion started by: lucshi09a
5 Replies

2. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

3. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

4. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

5. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

6. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

7. Shell Programming and Scripting

quick question

does anyone know what $? means? i echoed it on my box (running AIX Korn shell) and got 127 (2 Replies)
Discussion started by: penfold
2 Replies

8. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

9. Shell Programming and Scripting

Quick perl question

Hey everyone, I am a newbie with perl, and I just have a quick question that may seem really stupid. Like I said, I'm new, so please bear w/ me :) I'm trying to make a really simple program where there are two inputs. First one is just a string, and the next one is a number. Once the user... (7 Replies)
Discussion started by: jason_v
7 Replies

10. Shell Programming and Scripting

A very quick question

Just a super quick question: how do you put a link in your php code. I want to make a link to something in /tmp directory. i.e. how do you put a href into php, I think it's done a bit differently. thanks john (1 Reply)
Discussion started by: jmg5
1 Replies
Login or Register to Ask a Question