|
29 May 2003, 19:09
|
#1
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
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!!!
|
|
|
29 May 2003, 19:14
|
#2
|
Gubbish
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
|
No. There are ways to do it that doesn't take too long tho, but you need some looping.
__________________
Gubble gubble gubble gubble
|
|
|
29 May 2003, 21:20
|
#3
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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.
|
|
|
29 May 2003, 21:29
|
#4
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
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!!!
|
|
|
29 May 2003, 21:38
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
OK, I don't understand why you'd have a structure like that. How far are you in your perl book?
|
|
|
29 May 2003, 23:24
|
#6
|
Motherfracker
Join Date: May 2001
Posts: 2,985
|
Spinner is apparently a Perl 'god', ask him for help
|
|
|
29 May 2003, 23:45
|
#7
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
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!!!
|
|
|
30 May 2003, 00:49
|
#8
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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.
|
|
|
30 May 2003, 03:33
|
#9
|
Ngisne
Join Date: Jul 2001
Location: right here
Posts: 79
|
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
|
|
|
30 May 2003, 06:20
|
#10
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
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!!!
|
|
|
15 Jun 2003, 05:00
|
#11
|
Registered User
Join Date: Jun 2003
Location: New Jersey, USA
Posts: 1
|
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)
|
|
|
|
All times are GMT +1. The time now is 14:41.
| |