User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 29 May 2003, 19:09   #1
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Hokay, Perl questions start here :)

I got a book, now the silly questions

From what I understand it's possible to sort a multi dimensional array based on whatever 'hash'(?) you want.

e.g. if I have $planets['id']['score'] and $planets['id']['roids'] I can choose which to sort on and everything else stays linked to it's key (i.e. all the planet info stays linked to the correct $planets['id'] but the order is changed.

Have I got that right? If I have then I have to rewrite my PA dump script in Perl immediately

[edit] Excuse the PHP syntax, haven't got past reading the Perl stuff yet [/edit]
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 29 May 2003, 19:14   #2
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
No. There are ways to do it that doesn't take too long tho, but you need some looping.
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 29 May 2003, 21:20   #3
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by W
No. There are ways to do it that doesn't take too long tho, but you need some looping.
Huh? @id_sorted = sort {$a->{"id"} <=> $b->{"id"}} @planets;

Unless I've misunderstood.
queball is offline   Reply With Quote
Unread 29 May 2003, 21:29   #4
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by queball
Huh? @id_sorted = sort {$a->{"id"} <=> $b->{"id"}} @planets;

Unless I've misunderstood.
That makes sense but can I do it with say...

@id_sorted = sort {$a->{"id"} {"score"}<=> $b->{"id"}{"score"}} @planets;

???
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 29 May 2003, 21:38   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
OK, I don't understand why you'd have a structure like that. How far are you in your perl book?
queball is offline   Reply With Quote
Unread 29 May 2003, 23:24   #6
KaneED
Motherfracker
 
Join Date: May 2001
Posts: 2,985
KaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond reputeKaneED has a reputation beyond repute
Spinner is apparently a Perl 'god', ask him for help


KaneED is offline   Reply With Quote
Unread 29 May 2003, 23:45   #7
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by queball
OK, I don't understand why you'd have a structure like that. How far are you in your perl book?
Imagine the structure...

@planets['id']['score']
@planets['id']['rank']
@planets['id']['roids']
@planets['id']['roidrank']

Make sense now? Lots of subfields for each planet
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 30 May 2003, 00:49   #8
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Gayle29uk
Imagine the structure...

@planets['id']['score']
@planets['id']['rank']
@planets['id']['roids']
@planets['id']['roidrank']

Make sense now? Lots of subfields for each planet
Nope. Do I replace 'id' with a number?
Like,
$planets[5]{score}=56515;
$planets[5]{roids}=56;
?

Then,
$rank=1; $_{rank}=$rank++ foreach sort {$a{score} <=> $b{score}} @planets;
or somesuch. I don't get this ['id'] business.
queball is offline   Reply With Quote
Unread 30 May 2003, 03:33   #9
Atamur
Ngisne
 
Join Date: Jul 2001
Location: right here
Posts: 79
Atamur is an unknown quantity at this point
Re: Hokay, Perl questions start here :)

I think you need to be a bit more specific about the data structure you are trying to work with. Perl has 3 data structures. scalar, list, and hash. From your description you seem to want a hash of hashes. If so, you cannot sort that because a hash is an unordered structure. However, you can come up with a list of keys that will correspond to an ordered sequence of hash mappings. i.e.

Code:
%planets = (
'id1' => {'id'=>'id1', 'roids' => 3, 'score' => 10000},
'id2' => {'id'=>'id2', 'roids' => 6, 'score' => 15000}
);

@roidIDs = map {$_->{'id'}} sort {$a->{'roids'} <=> $b->{'roids'}} values %planets;
This is a strange way of organizing this data though. What queball suggested (a list of hashes) is much more straightforward.
__________________
down with signatures
Atamur is offline   Reply With Quote
Unread 30 May 2003, 06:20   #10
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by queball
Nope. Do I replace 'id' with a number?
Like,
$planets[5]{score}=56515;
$planets[5]{roids}=56;
?

Then,
$rank=1; $_{rank}=$rank++ foreach sort {$a{score} <=> $b{score}} @planets;
or somesuch. I don't get this ['id'] business.
Yes, id is a number think of it as a primary key sort of thing

Otherwise I'd end up with a list of scores nicely sorted in order but no idea whatsoever which planet they belonged to
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 15 Jun 2003, 05:00   #11
DarknessX
Registered User
 
Join Date: Jun 2003
Location: New Jersey, USA
Posts: 1
DarknessX is an unknown quantity at this point
I think good reads around perldoc perlref and perldoc perlreftut are in order. You want to create hashrefs (or arrayrefs) to scalars in your code. After you've taken a gander at the perldocs, try reading this: http://www.perlmonks.org/index.pl?node_id=90647 (How to handle multidimensional arrays)
DarknessX is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 14:41.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018