Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by InfiniteSilence (Curate)
on Jun 09, 2014 at 17:59 UTC ( #1089300=note: print w/replies, xml ) Need Help??


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 (Sage) 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 (Bishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1089300]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-01-18 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (52 votes). Check out past polls.

    Notices?