[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sorting

Posted on 1998-09-10
11
Medium Priority
?
185 Views
Last Modified: 2010-03-05
I want to sort an array of arrays.

I use the line

sort byName @records;

where byName is defined as

sub byName{
      return $records[$a]->[$SITENAME] cmp $records[$b]->[$SITENAME];
}

Please explain why this doesn't work.
0
Comment
Question by:rafistern
[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
  • 5
  • 5
11 Comments
 

Expert Comment

by:arhnold
ID: 1204798
Just to clarify:  Do you want each subarray sorted within itself or are you trying to do some sort of sorting between the subarrays?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204799
try
  @records[ sort byName (0..$#records)];
or
  sort {$a->[$SITENAME] cmp $b->[$SITENAME]} @records;
0
 
LVL 4

Author Comment

by:rafistern
ID: 1204800
arnhold,
I have a set of arrays which contain dat that I read from a flat file database. I want to sort the arrays so that they will be in ascending order sorted by a key field (the name of the item to which all the data in the record refers).

ozo,
I will try your suggestions.

Thanks,
Rafi
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 4

Author Comment

by:rafistern
ID: 1204801
ozo,
It doesn't work. Anyone know what the correct syntax is for this operation? I could always write my own sorting routine.. but it's a shame not to use the existing function "sort".
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204802
What doesn't work.
How is @records defined?
0
 
LVL 4

Author Comment

by:rafistern
ID: 1204803
I just found the answer myself.
@records= was missing at the beginning of the line!
What a stupid mistake!!

Thanks, guys.
0
 
LVL 4

Author Comment

by:rafistern
ID: 1204804
P.S.
ozo, I will give you points for your answer because your syntax was correct although you didn't find my other mistake. Post me a blank answer.
0
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 1204805

0
 
LVL 84

Expert Comment

by:ozo
ID: 1204806
> you didn't find my other mistake
How could I, when you didn't tell me you wanted to replace @records with the sorted array?
That's why I asked what wasn't working.
0
 
LVL 4

Author Comment

by:rafistern
ID: 1204807
It's not really worth arguing about but the line which I posted:

sort byName @records;

has got to be just one of the most useless lines of code as it doesn't return anything. Thanks for the correct syntax for the sorting function. It's now working fantastically.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204808
If that's the complete statement, yes, and perl -w will even warn you about that.
But besides assigning it to @records, you might have wanted to return it from a sub, or assign it to something else, or print it, or map it, or foreach over it, or pass it to another function...
Anyway, I'm glad it's working for you now.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

649 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