Solved

reverse data read.

Posted on 1998-11-11
2
157 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…

713 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