Solved

reverse data read.

Posted on 1998-11-11
2
148 Views
Last Modified: 2010-03-04
Basically I am reading data in from a flat-file that looks like this:  (format like $itemid|$reportdate|)

0|10/1/1998|
1|11/08/1998|
2|11/10/1998|
3|11/25/1998|
4|12/01/1998|
5|12/05/1998|
6|12/6/1998|
7|1111|

using code like this:

open(adb,$resourcedb);
while (<adb>) {
open (AFILE,"$resourcedb") || die "Content-type: text/html\n\nCan't Open $resourcedb(r): $!\n";
@LINES=<AFILE>;
close(AFILE);
$SIZE=@LINES;
for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
($itemid, $reportdate) = split(/\|/,$_);
print "<A HREF=\"reports/$itemid\">$reportdate\</A><BR>\n";
}
close(adb);
}


BUT - I don't want to read/print from top down, I would prefer it be in reverse order i.e. reading/printing the last record (record 7) first then going back to record 0 - any easy way to do this off the top of your head?

0
Comment
Question by:asago
2 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1206151
Well, you could do:

for ($i = $SIZE; $i >= 0; $i--)

Or, you could do

@LINES = reverse(@LINES)

But you really better hope that @LINES is relatively small.  This could get ugly (as could the @LINES = <AFILE> ) as the size of the array in memory approaches the size of your physical memory.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206152
for( reverse <AFILE> ){
  ($itemid, $reportdate) = split/\|/;
   print qq?<A HREF="reports/$itemid">$reportdate</A><BR>\n?;
}

#but why are you doing this looping over each line of <adb>?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now