I have two arrays and i wish to sort the first one numerically, but after
sorting, I would like the second array to be in the same matching order as
the first array.
ie.
@l1={3,1,2};
@l2={'a','b','c '};
@l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
# do something to @l2 to make order {'b','c','a'} (preserving the original
mapping with the first list)
There's probably an easy way to do this that i'm not aware of.
Thanks in advance,
Brett. 4 5312
Brett wrote: I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array.
ie.
@l1={3,1,2}; @l2={'a','b','c '};
@l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
# do something to @l2 to make order {'b','c','a'} (preserving the original mapping with the first list)
There's probably an easy way to do this that i'm not aware of.
Indeed, there is: use a data structure that matches your problem better.
Instead of having a pair of unrelated arrays use a single array of pairs.
And then sort that single array by the value of the first component of each
pair.
jue
"Jürgen Exner" <ju******@hotma il.com> wrote in message
news:8Tmee.2706 $Vu.1954@trnddc 07... Brett wrote: I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array.
ie.
@l1={3,1,2}; @l2={'a','b','c '};
@l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
# do something to @l2 to make order {'b','c','a'} (preserving the original mapping with the first list)
There's probably an easy way to do this that i'm not aware of. Indeed, there is: use a data structure that matches your problem better. Instead of having a pair of unrelated arrays use a single array of pairs. And then sort that single array by the value of the first component of
each pair.
jue
I'm new to this, and tried to give it a go.
struct ID =>
{
number => '$',
name => '$',
};
my @IDs = ID->new();
#fill data...
@IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on number
but that failed to sort the list as i expected. How can i use sort to do
what I want?
In article <XZ************ ********@adelph ia.com>, Brett
<bg*****@hotmai l.com> wrote: "Jürgen Exner" <ju******@hotma il.com> wrote in message news:8Tmee.2706 $Vu.1954@trnddc 07... Brett wrote: I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array.
ie.
@l1={3,1,2}; @l2={'a','b','c '};
@l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
# do something to @l2 to make order {'b','c','a'} (preserving the original mapping with the first list)
There's probably an easy way to do this that i'm not aware of.
Indeed, there is: use a data structure that matches your problem better. Instead of having a pair of unrelated arrays use a single array of pairs. And then sort that single array by the value of the first component of each pair.
jue
I'm new to this, and tried to give it a go.
struct ID => { number => '$', name => '$', };
my @IDs = ID->new(); #fill data...
@IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on number
but that failed to sort the list as i expected. How can i use sort to do what I want?
Please post a complete program so we can see where you are going wrong,
but post it to comp.lang.perl. misc because this newsgroup is defunct.
Here is a version that uses an array of array references to an array of
two elements, as Jürgen suggested:
#!/usr/local/bin/perl
use strict;
use warnings;
my @l1 = qw/ 3 1 2 /;
my @l2 = qw/ a b c /;
my @ids = map { [ $l1[$_], $l2[$_] ] } (0..$#l1);
print "Unsorted Array:\n";
for my $r ( @ids ) {
print " @$r\n";
}
print "\nSorted array:\n";
foreach my $r ( sort { $a->[0] <=> $b->[0] } @ids ) {
print " @$r\n";
}
__OUTPUT__
Unsorted Array:
3 a
1 b
2 c
Sorted array:
1 b
2 c
3 a
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
"Jim Gibson" <jg*****@mail.a rc.nasa.gov> wrote in message
news:0505200512 41241521%jg**** *@mail.arc.nasa .gov... In article <XZ************ ********@adelph ia.com>, Brett <bg*****@hotmai l.com> wrote:
"Jürgen Exner" <ju******@hotma il.com> wrote in message news:8Tmee.2706 $Vu.1954@trnddc 07... Brett wrote: > I have two arrays and i wish to sort the first one numerically, but > after sorting, I would like the second array to be in the same > matching order as the first array. > > ie. > > @l1={3,1,2}; > @l2={'a','b','c '}; > > @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3} > > # do something to @l2 to make order {'b','c','a'} (preserving the > original mapping with the first list) > > There's probably an easy way to do this that i'm not aware of.
Indeed, there is: use a data structure that matches your problem
better. Instead of having a pair of unrelated arrays use a single array of
pairs. And then sort that single array by the value of the first component of each pair.
jue
I'm new to this, and tried to give it a go.
struct ID => { number => '$', name => '$', };
my @IDs = ID->new(); #fill data...
@IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on
number but that failed to sort the list as i expected. How can i use sort to do what I want?
Please post a complete program so we can see where you are going wrong, but post it to comp.lang.perl. misc because this newsgroup is defunct.
Here is a version that uses an array of array references to an array of two elements, as Jürgen suggested:
#!/usr/local/bin/perl
use strict; use warnings;
my @l1 = qw/ 3 1 2 /; my @l2 = qw/ a b c /;
my @ids = map { [ $l1[$_], $l2[$_] ] } (0..$#l1);
print "Unsorted Array:\n"; for my $r ( @ids ) { print " @$r\n"; } print "\nSorted array:\n"; foreach my $r ( sort { $a->[0] <=> $b->[0] } @ids ) { print " @$r\n"; }
__OUTPUT__
Unsorted Array: 3 a 1 b 2 c
Sorted array: 1 b 2 c 3 a
Thanks, i'll report to the other group. I'd like to stick with the
structures, since they look a bit easier.
Brett. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: googleboy |
last post by:
I didn't think this would be as difficult as it now seems to me.
I am reading in a csv file that documents a bunch of different info on
about 200 books, such as title, author, publisher, isbn, date and
several other bits of info too.
I can do a simple sort over the first field (title as it turns out),
and that is fine as far as it gets:
|
by: James P. |
last post by:
Hello,
I have a report with the Priority field is used as sort order and
grouping. The problem is the data in this Priority field if sorted in
ascending order is: High, Low, and Medium. How could I sort it as:
Low, Medium, High?
Any suggestion is greatly appreciated,
James
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy *enemydata; // Holds information about an enemy (in a game)
// Its a double linked list node
|
by: Majnu |
last post by:
Hi community,
just in case somebody needs a shellsort in c#, I rewrote the pascal
code that I found in another newsgroup. Here are both. For more
explanation on the pascal code you can search for the post on google.
I was interested in the shell sort because I needed to sort long lists
of data that may already be in order. For that reason I didn't want to
use the arraylist's sort method, which uses quick sort (and has a
worst case for...
|
by: Leon |
last post by:
I have six textbox controls on my webform that allows the user to enter any
numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. How can
accomplish this task? Thanks!
| |
by: Gummy |
last post by:
Hello All,
I have a webpage that has two dropdown listboxes. Based on what is selected
in these dropdown listboxes, it filters a DataGrid . That works fine.
In the DataGrid , when I go to edit a row, I change the textbox (or other
control), click Update, but it doesn't save my data. I then did this...
In Page_Load, I added code to filter the DataGrid only when it was Not
Page.IsPostBack. Then the editing/updating of the DataGrid data...
|
by: Mark |
last post by:
In a specification I'm designing, I'd like to specify that the order
of Names (IDs) in an attribute value of datatype IDREFS is
significant.
My question is if most real-world XML parsers preserve the order of
IDs in IDREFS? If most don't, then specifying the significance of
order is problematic.
(As far as I can tell, the XML 1.0 spec is silent on this topic.)
|
by: xahlee |
last post by:
Last year, i've posted a tutorial and commentary about Python and
Perl's sort function. (http://xahlee.org/perl-python/sort_list.html)
In that article, i discussed a technique known among juvenile Perlers
as the Schwartzian Transform, which also manifests in Python as its
“key†optional parameter.
Here, i give a more detailed account on why and how of this construct.
----
|
by: aRTx |
last post by:
I have try a couple of time but does not work for me
My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?
The Script
<?
/*
ORIGJINALI
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |