Is this the same question as Reading the contents of a file from the bottom up? If so, then it would have been better to post it in a single thread.
If your file is small enough to be read entirely into memory, then note that you can iterate over an array in both directions:
use warnings; use strict; my @array = qw/ abc def ghi /; for my $x (@array) { print "$x\n"; } for my $x (reverse @array) { print "$x\n"; } while (@array) { my $x = shift @array; print "$x\n"; } @array = qw/ abc def ghi /; while (@array) { my $x = pop @array; print "$x\n"; }
See reverse, shift, and pop. However, unless the files are short, reading an entire file into memory is usually less efficient than reading a file line-by-line, and stevieb showed one solution to reading the file backwards in the other thread.
Also, note that your if-elsif chain would probably be better written as a hash. Basically, $hash{$match}++ would create a hash where the keys are the digits, and the values are the counters. If you wanted to have the keys be named after the digits, like your variables, a second hash would help, e.g. my %digits = ( 1=>'one', 2=>'two', ... ); ... $hash{$digits{$match}}++. Also, in your code you're using fixed array indicies ($lines[12] and @lines[0..11]), but you're shrinking the array with shift, so that won't be accessing the same array elements. You can use $#array to get the index of the last element of the array (-1 if it is empty), and you can say $array[-1] to access the last element of the array, $array[-2] for the second-to-last, etc.
If you want help with your algorithm, please show some sample input and the expected output for that input, along with a runnable piece of code, in a single node. See also How do I post a question effectively? and Short, Self-Contained, Correct Example.
In reply to Re: Reversing the action of the while loop
by haukex
in thread Reversing the action of the while loop
by Scotmonk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |