Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 4th, 2001, 08:12 AM
d0g1e d0g1e is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: The Netherlands
Posts: 8 d0g1e User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to d0g1e
Question

Imagine this:

@NewsID = (1,2,3,4,5);
@NewsViews = (28,24,13,67,12);

and that $NewsViews[0] is the number of times that $NewsID[0] has been viewed.

Now I want to sort them, so that I get an array starting with the lowest view-rate.

When I do a @SortedNewsViews=sort(@NewsViews),
the array @NewsViews is sorted, but the array @NewsID remains the same. Now $NewsViews[0] is NOT the number of times that $NewsID[0] has been viewed.

So how do I sort @NewsViews together with @NewsID?

Reply With Quote
  #2  
Old May 4th, 2001, 10:21 AM
Atrus's Avatar
Atrus Atrus is offline
yet another member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 262 Atrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hi!

What about using a hash for this?

Like
Code:
 %News = (
                1 => 28,
                2 => 24,
                3 => 13,
                [...]
   );


This way id and value are tied together and may be sorted at will either by id or by value.


Greetings

Roland.

Reply With Quote
  #3  
Old May 4th, 2001, 12:23 PM
d0g1e d0g1e is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: The Netherlands
Posts: 8 d0g1e User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to d0g1e
Unhappy I don't quite get it...

I don't quite get it?

If I'd do a @SortedNews = sort(keys(%News))
I'd end up with any array with 13, 24, 28??

Reply With Quote
  #4  
Old May 4th, 2001, 02:19 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Talking USE A HASH!!!

You want to be using a hash for this. That will accomplish what I think it is you are trying to accomplish.

Right now you have no guarantees that the item with ID #1 will have the lowest viewing rate so you can't expect them to correspond when you sort the arrays.

Try this:
It outputs to STDOUT but I'm sure you can tweak it to drop the info into a new hash:

Code:
%News = (
    1 => 28,
    2 => 24,
    3 => 13,
    4 => 67,
    5 => 12,
);

foreach $key ( sort { $News{$a} <=> $News{$b} } keys %News ) {
    print $News{$key}, "\t$key\n";
}
__________________
- dsb -
Perl Guy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > sorting 2 arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT