Gtforce has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unscalar'ize a fake array
by Athanasius (Archbishop) on Sep 17, 2017 at 12:48 UTC | |
by Gtforce (Sexton) on Sep 22, 2017 at 02:23 UTC | |
|
Re: Unscalar'ize a fake array
by 1nickt (Canon) on Sep 17, 2017 at 12:36 UTC | |
by Gtforce (Sexton) on Sep 22, 2017 at 01:57 UTC | |
|
Re: Unscalar'ize a fake array
by kcott (Archbishop) on Sep 18, 2017 at 05:26 UTC | |
by Gtforce (Sexton) on Sep 22, 2017 at 02:15 UTC | |
|
Re: Unscalar'ize a fake array
by karlgoethebier (Abbot) on Sep 17, 2017 at 13:55 UTC | |
|
Re: Unscalar'ize a fake array
by Marshall (Canon) on Sep 17, 2017 at 18:55 UTC | |
by Gtforce (Sexton) on Sep 22, 2017 at 01:53 UTC | |
by Marshall (Canon) on Sep 25, 2017 at 20:26 UTC |