Solved

reverse data read.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

11 Experts available now in Live!

Get 1:1 Help Now