Another slight variation. Use foreach my $line (@array){...} if you have an array like this.

Update: I looked back at this thread and I admit to being flummoxed by what a "fake array" could be? Question: Do you have an array or not? That is a yes or no question. I'm not sure at all what a "fake" array could be? There could be a sequence of lines in a file. There could be multiple lines contained in a scalar text variable. I wouldn't characterize either of those situations as a "fake" array. I've worked with a lot of students in various programming languages, but I've never heard anybody refer to a "fake" array. You either have an array or you don't. If it is not an array, then there is a better more precise CS word for what this is.

#!/usr/bin/perl use strict; use warnings; my $total = 0; my $num = 0; while (my $line = <DATA>) { my @tokens = split ' ', $line; # split removes line ending next unless @tokens == 3; # ignore blanks and header line $total += $tokens[-1]; # add last column $num++; } print "total = $total Average=",$total/$num,"\n"; # prints: total = 127297 Average=31824.25 __DATA__ CHECKPOINT 2017-08-01 20MICRONS 37744 2016-08-01 20MICRONS 25966 2016-04-20 20MICRONS 30807 2016-04-01 20MICRONS 32780

In reply to Re: Unscalar'ize a fake array by Marshall
in thread Unscalar'ize a fake array by Gtforce

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.