sorting delimited file in perl

Posted on 2003-02-22
Medium Priority
Last Modified: 2013-12-25
Here is a peice of my code

open (ORGDB,"<$database");
close (ORGDB);

foreach $rec (@ODB){
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
Question by:derek4716
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

Expert Comment

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

@ODB = sort(@ODB);


@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 = 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.

Author Comment

ID: 8000560
thanks for the response ShaharEvron,

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

Accepted Solution

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

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