in reply to Unscalar'ize a fake array
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Unscalar'ize a fake array
by Gtforce (Sexton) on Sep 22, 2017 at 01:53 UTC | |
by Marshall (Canon) on Sep 25, 2017 at 20:26 UTC |