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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.