Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by pimperator (Acolyte)
on Jun 09, 2014 at 17:07 UTC ( [id://1089291]=perlquestion: print w/replies, xml ) Need Help??

pimperator has asked for the wisdom of the Perl Monks concerning the following question:

O' gracious Monks we have heard on high. Swiftly coding o'er the cubicles

PerlMonks, I throw myself at your feet. I am worthless

I've only started using use strict; and this error eludes me. Yes Monks, I tried googling it and I don't understand why this is still happening.

The output reads as such,

1 key Can't use string ("101") as an ARRAY ref while "strict refs" in use at + arry_test.pl line 38.
This is the subroutine I'm working on.
#!/usr/bin/perl use warnings; use strict; my @a = (1..12864); &chunky(@a); sub chunky { my %master; my @temp; my @input = @_; my $chunks = int(scalar(@_)/100); my $carryOver=0; my $tipping; my $i; my $k; my $keyed; for($i=1;$i<=$chunks;$i++){ $tipping = $carryOver+100; for($k=$carryOver;$k<=$tipping;$k++){ push(@temp,$input[$k]); if($k==$tipping){ $carryOver=$k;} } $keyed = $i." key"; $master{$keyed}=@temp; undef @temp; undef $keyed; } foreach my $key (sort keys %master){ print $key."\n"; foreach( @{$master{$key}} ){ print $_."\t"; } print "\n"; } }

TUSEN TAKK MONKS

I FIGURED IT OUT

#!/usr/bin/perl use warnings; use strict; my @a = (1..1284); &chunky(@a); sub chunky { my %master; my @temp; my @input = @_; my $chunks = int(scalar(@_)/100); my $carryOver=0; my $tipping; my $i; my $k; my $keyed; for($i=1;$i<=$chunks;$i++){ $tipping = $carryOver+100; for($k=$carryOver;$k<=$tipping;$k++){ push(@temp,$input[$k]); if($k==$tipping){ $carryOver=$k;} } $keyed = $i." key"; $master{$keyed}=[@temp]; undef @temp; undef $keyed; } foreach my $key (sort keys %master){ print $key."\n"; foreach( @{$master{$key}} ){ print $_."\t"; } print "\n"; } }

Should have learned about arrayrefs and such I suppose

Replies are listed 'Best First'.
Re: Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...
by toolic (Bishop) on Jun 09, 2014 at 17:16 UTC

    Tip #4 from the Basic debugging checklist:

    use Data::Dumper; print Dumper(\%master);
    This shows that %master is a simple hash, not a hash-of arrays as your code expects.

    Perhaps you should change:

    $master{$keyed}=@temp;

    to:

    $master{$keyed} = \@temp;
Re: Can't use string ("...") as an ARRAY ref while "strict refs" in use at x.pl line ...
by Bloodnok (Vicar) on Jun 09, 2014 at 17:16 UTC
    Hmm, instead of $master{$keyed}=@temp;, did you not mean to write $master{$keyed}=\@temp;? As you've actually written it, a scalar is created as the value, instead of the array reference that you apparently meant.

    A user level that continues to overstate my experience :-))
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

    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

      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)

      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: perlquestion [id://1089291]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found