http://qs321.pair.com?node_id=1089300


in reply to Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...

Code rewrite

use warnings; use strict; use Data::Dumper; my @a = (1..1284); my $chunkedRef = chunky(@a); sub chunky { #for chunks (chunks = 100 blocks of passed -in array) + + my %master; for(0..int @_/100) { # create an array for each 100 elements and add it to the has +h + push @{$master{$_+1 . ' key'}},@a[$_*100..$_*100+100]; } return \%master; } print Dumper %$chunkedRef; 1;

Celebrate Intellectual Diversity

  • Comment on Re: Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...
  • Download Code

Replies are listed 'Best First'.
Re^2: Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...
by LanX (Saint) on Jun 09, 2014 at 19:22 UTC
    Because ranges are inclusive! :)

    This @a[$_*100..$_*100+100]; should rather have a +99 to avoid overlaps.

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re^2: Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...
by AnomalousMonk (Archbishop) on Jun 09, 2014 at 18:49 UTC

    Why does each chunk appear to have 101 elements?

    c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dumper; my @a = (1..1284); my $chunkedRef = chunky(@a); sub chunky { my %master; for(0..int @_/100) { push @{$master{$_+1 . ' key'}},@a[$_*100..$_*100+100]; } return \%master; } print 'elements per chunk: ', scalar @{ $chunkedRef->{'1 key'} }; " elements per chunk: 101