?
Solved

sorting delimited file in perl

Posted on 2003-02-22
3
Medium Priority
?
167 Views
Last Modified: 2013-12-25
Here is a peice of my code
*************************************
print "HTML GOES HERE";

open (ORGDB,"<$database");
@ODB=<ORGDB>;
close (ORGDB);

foreach $rec (@ODB){
   chomp($rec);
  ($productId,$itemName,$description)=split(/\|/,$rec);
print "<tr><td>$productID</td><td>$itemname<td></td><td>$description</td></tr>\n";
}
***************************************

How do I sort the data by itemname from a-z before it gets printed out to the page?  I dont want to rewrite the file in the new order, i just want the data to show up on the page in order
0
Comment
Question by:derek4716
[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
3 Comments
 

Expert Comment

by:ShaharEvron
ID: 8000177
What you should do is sort @ODB after reading it, mybe something like :

@ODB = sort(@ODB);

or:

@SortedODB = sort(@ODB);

Anyway, remember that sort works according to ASCII code, and this means that 'Z'(uppercase) comes before 'a'(lowercase). If you don't care about the case, you can use lc, uc or preferably ucfirst to change the case of strings..
for example:

@ODB = <ORGDB>;
grep(ucfirst($_),@ODB);
@ODB = sort(@ODB);

This is not a perfect solution and there are better ones, after all perl was made for this SORT (he he) of tasks, but I'm too tired to think of it. Hope this helps anyway.
0
 

Author Comment

by:derek4716
ID: 8000560
thanks for the response ShaharEvron,

I need to sort by the itemname feild though, I forgot to mention that, sorry.
0
 
LVL 84

Accepted Solution

by:
ozo earned 800 total points
ID: 8002217
@ODB = sort{ (split/\|/,$a)[1] cmp (split/\|/,$b)[1] } @ODB;
#or
@ODB = map{ $_->[0] }
       sort{ $_->[1] cmp $_->[1] }
       map{ [$_,(split/\|/)[1]] }
       @ODB;
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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