Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Cant use string(_) as array ref..!!

by reaper9187 (Scribe)
on Jan 24, 2013 at 09:07 UTC ( #1015108=perlquestion: print w/ replies, xml ) Need Help??
reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone ..
I'm working on a problem.. Jus need some help with the following code:
use Data::Dumper; use strict; our (@a,$c,$key,$bar); #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Tk; use Tk::Chart::Lines; my $mw = MainWindow->new( -title => 'Tk::Chart::Lines example', -background => 'white', ); my $chart = $mw->Lines( -title => 'My graph title', -xlabel => 'X Label', -ylabel => 'Y Label', )->pack(qw / -fill both -expand 1 /); @a = qw/Mon Tue Wed Thurs/; foreach $key(@a) { push(@{${$key}[0]},1,33); push(@{${$key}[0]},22,11); push(@{${$key}[1]},2,5); push(@{${$key}[2]},3,4); } $c ="Mon"; # Add a legend to the graph my @legends = ( 'legend 1', 'legend 2', 'legend 3' ); $chart->set_legend( -title => 'Title legend', -data => \@legends, -titlecolors => 'blue', ); # Add help identification $chart->set_balloon(); print Dumper(@$c); # Create the graph $chart->plot( @$c ); MainLoop();
I get the error.. "Cant use string "Mon" as array ref while strict refs in use ... "

How do i solve this issue .. Please help ..!!!

Comment on Cant use string(_) as array ref..!!
Download Code
Replies are listed 'Best First'.
Re: Cant use string(_) as array ref..!!
by choroba (Canon) on Jan 24, 2013 at 09:17 UTC
    Let's analyze it. @a is an array of strings. Next, you iterate over it with
    foreach $key (@a)
    So, $key is a string, "Mon" in the first iteration. Then, you use the expression
    ${$key}[0]
    It tries to dereference $key as an array reference, but it is not: it is a scalar string. Hence the error.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Cant use string(_) as array ref..!!
by Anonymous Monk on Jan 24, 2013 at 09:14 UTC
Re: Cant use string(_) as array ref..!!
by thundergnat (Deacon) on Jan 24, 2013 at 14:33 UTC

    Update: changed 'indirect' to 'symbolic' as that is what I meant, but blanked out on while I was composing my post.

    First off, Kudos for using the strict and warnings pragmas. They will really help you catch typos and silly errors in your script.

    push(@{${$key}[0]},1,33); push(@{${$key}[0]},22,11); push(@{${$key}[1]},2,5); push(@{${$key}[2]},3,4);

    Ok you are trying to use indirect symbolic referencing to autovivify your hashes and that is not allowed under the strict pragma. You would probably be better off just predeclaring a single hash and use the day names as keys. Did you really want a 4 element array at index 0 and 2 element arrays elsewhere? Tk::Chart::Lines is going to complain about that. Anyway see if this clears anything up for you.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Tk; use Tk::Chart::Lines; my $mw = MainWindow->new( -title => 'Tk::Chart::Lines example', -background => 'white', ); my $chart = $mw->Lines( -title => 'My graph title', -xlabel => 'X Label', -ylabel => 'Y Label', )->pack(qw / -fill both -expand 1 /); my @a = qw/Mon Tue Wed Thurs/; my %days; foreach my $key (@a) { push @{$days{$key}}, [1, 33]; push @{$days{$key}}, [22, 11]; push @{$days{$key}}, [2, 5]; push @{$days{$key}}, [3, 4]; } my $c ='Mon'; # Add a legend to the graph my @legends = ( 'legend 1', 'legend 2', 'legend 3' ); $chart->set_legend( -title => 'Title legend', -data => \@legends, -titlecolors => 'blue', ); # Add help identification $chart->set_balloon(); #print Dumper \$days{$c}; # Create the graph $chart->plot( $days{$c} ); MainLoop;
Re: Cant use string(_) as array ref..!!
by reaper9187 (Scribe) on Jan 24, 2013 at 09:22 UTC
    Can you please suggest a way to remove this issue ????

      Just for clarification can you tell me what you are doing here ? push(@{${$key}[0]},1,33);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015108]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2015-07-31 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (281 votes), past polls