I receive an array @result, which is my starting point. I think it is an array, but when I run this snippet of code to test the data that is in the array, it would appear that there's a scalar sitting in it. My end-purpose is to calculate the average of the last column, but can't figure out how. Any help is appreciated, thanks.
my $counter=0; foreach my $element (@result) { print "\nCHECKPOINT\n $result[$counter]\n"; $counter ++; }
Result:
CHECKPOINT
2017-08-01 20MICRONS 37744
2016-08-01 20MICRONS 25966
2016-04-20 20MICRONS 30807
2016-04-01 20MICRONS 32780
UPDATE: I don't mean to prolong this thread, but hopefully a quick question. My data set is about 100Mb with about 2k records and consequently ((2k-n)*n) loops, 'n' being my averaging period (loops because I'm doing a moving average to smoothen the data series). I tested my data set using a "WHILE", and then using a "FOREACH" (the 2 suggestions that were provided on this thread). Looping via foreach costs me less than a second, whilst looping via while costs several seconds (~5 seconds). I'm new to perl and am wondering if that's how it is meant to be, and consequently, what would be my use case for 'while'.
foreach (<myarray>) { my @dataset = split /blah/, $myarray[$outercounter]; for ($innercounter = 0; $innercounter < n; $innercounter++) { my $counter = $innercounter + $outercounter; my @miniarray = split /blah/, $myarray[$counter]; $sum += $miniarray[x]; } $outercounter ++; my $mavg = $sum / n; push (@resultset, "the things I need"); $sum = 0; $mavg = 0; last if $outercounter == scalar(@myarray); }
In reply to Unscalar'ize a fake array by Gtforce
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |