in reply to Reversing the action of the while loop

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.

Replies are listed 'Best First'.
Re^2: Reversing the action of the while loop
by Scotmonk (Sexton) on Nov 24, 2019 at 21:23 UTC
    Thankyou both for that and the advice. I am a complete newbie to this PERL programming.

    I have statistical work to do, which I had tried to do with excel, but PERL seems to give me more flexibility and simplicity, also I have upto 10 million samples which Excel cant handle in one go.

    I am a neuroscientist working on the random generation of values in which we are trying to mimick a model from nature. The more data samples I have, the more acurate my calculations are.
    I can generate the random elements great, but processing them is tricky while trying to learn PERL at the same time.
    I have the 7th edition camel book, and as it says in there, as a beginner I am struggling with the efficiency of the written code.

    Also, as you have recognised, part of my problem is asking a question in a way that others understand.

    The last two questions that I asked are similar but I was thinking I might try the code in two different ways. Maybe betterjust to tell you what I am trying to do in a new post as you suggest.
      Although mostly irrelevant to the Perl-oriented topic here, it is worth noting that Excel does have the ability to include database queries ... so called Power Query or Get and Transform. So, there are ways to handle database-sized things directly in Excel, such as a dataset of 10 million rows.
        This is what you sound like.
        A reply falls below the community's threshold of quality. You may see it by logging in.