Solved

reverse data read.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Six Sigma Control Plans

623 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