Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sorting one array, matching the second one

Posted on 2009-03-30
7
Medium Priority
?
172 Views
Last Modified: 2012-05-06
I have two arrays of strings, the left one needs to be natural sorted, I can do this however array2 has to be modified to maintain the index consistency.

@array1   @array2
1 --> b      1 --> bla
2 --> d      2 --> bla2
3 --> a1    3 --> bla3
4 --> a10  4 --> bla4
5 --> a20  5 --> bla5
6 --> a2    6 --> bla6

1 --> a1    1 --> bla3
2 --> a2    2 --> bla6
3 --> a10  3 --> bla4
4 --> a20  4 --> bla5
5 --> b      5 --> bla
6 --> d      6 --> bla2

i'm using a modified version of tye algorithm found on perkmonkey site for the natsort on array1 but then how would I order array2.

my @sorted = @list[
    map { unpack "N", substr($_,-4) }
    sort
    map {
        my $key = $list[$_];
        $key =~ s[(\d+)][ pack "N", $1 ]ge;
        $key . pack "N", $_
    } 0..$#list
];

Would you use a two dimensional array instead?
I want to make sure i'm doing this the most efficient way as the list can get long, any help appreciated
0
Comment
Question by:NewtonianB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24021882
I think a 2D array (or actually an array-of-arrays) would be better.  Here is an example:

my @array1 = (qw(b d a1 a10 a20 a2));
my @array2 = (qw(bla bla2 bla3 bla4 bla5 bla6));
 
##### Build array of array
my @AoA;
for(my $i=0; $i<=$#array1; $i++) {
	push @AoA, [$array1[$i], $array2[$i]];
}
 
##### Sort
my @AoA_sorted = sort {$a->[0] cmp $b->[0]} @AoA;
 
##### get original arrays, but sorted
@array1 = map{$_->[0]} @AoA_sorted;
@array2 = map{$_->[1]} @AoA_sorted;

Open in new window

0
 

Author Comment

by:NewtonianB
ID: 24022150
Thanks Adam! but It needs to be natural sorted instead of sort which is what's creating my complication, how would I use the above natural sorting algorithm or another one with your code
example of proper sort: a1 b1 a10 b1 b20  instead of  a1 a10 b1 b20
0
 

Author Comment

by:NewtonianB
ID: 24022460
i meant for example a1 a2 a3 a10 a12  instead of  a1 a10 a12 a2 a3
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24022640

##### Build array of array
my @AoA;
for(my $i=0; $i<=$#array1; $i++) {
	my $key = $array1[$i];
	$key =~ s/(\d+)/pack "N", $1/ge;
	push @AoA, [$key, $array1[$i], $array2[$i]];
}
 
##### Sort
my @AoA_sorted = sort {$a->[0] cmp $b->[0]} @AoA;
 
##### get original arrays, but sorted
@array1 = map{$_->[1]} @AoA_sorted;
@array2 = map{$_->[2]} @AoA_sorted;

Open in new window

0
 

Author Comment

by:NewtonianB
ID: 24023289
that should work! thanks!

Last quick question,
What's the most efficient way to get a 2Dimensional array consisting of the last two columns of array AoA_sorted?

could i just do something like
my @arrayTwoDim = map{$_->[1], $_->[2]} @AoA_sorted;
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 2000 total points
ID: 24023543
If you want to make a new @arrayTwoDim (you need the extra square brackets):
    my @arrayTwoDim = map{[$_->[1], $_->[2]]} @AoA_sorted;

If you want to change @AoA_sorted:
    shift @$_ foreach (@AoA_sorted);
0
 

Author Closing Comment

by:NewtonianB
ID: 31564469
I could not have asked for a better help. Thank you so much!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question