Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

can't use string as an array ref

by fionbarr (Friar)
on Mar 26, 2015 at 13:58 UTC ( [id://1121393]=perlquestion: print w/replies, xml ) Need Help??

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

Would someone please explain what't going on here?
Can't use string ("1") as an ARRAY ref while "strict refs" in use at r +ead_data.pl line 453.at read_data.pl line 455. foreach my $line ( @{ $server_list_hash{$server} } ) {

Replies are listed 'Best First'.
Re: can't use string as an array ref
by BrowserUk (Patriarch) on Mar 26, 2015 at 14:01 UTC

    $server_list_hash{$server} must be returning the string '1', rather than an array reference as you're expecting.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: can't use string as an array ref
by Preceptor (Deacon) on Mar 26, 2015 at 15:03 UTC

    Most likely this means you've inserted an array into a hash like this:

    my $somearray = qw ( servername ); $server_list_hash{$server} = @somearray;

    Which hasn't done what you thought, because it's done an assignment in a scalar context, which means it's assigned the number of elements in @somearray.

    What you would actually need to do would be:

    $server_list_hash{$server} = [@somearray];

    Be careful with something like:

    $server_list_hash{$server} = \@somearray;

    Because this will break if you reuse @somearray (e.g. it's not lexically scoped) because you'll be using the same reference each time

      $server_list_hash{$server}[ $server_count{$server}++ ] = "$role|$cto|$ +app_manager|$location";
      there are duplicate servers and I'm trying to put them in an array
        Are you sure that's the only write access happening to your hash? This sounds like a good time for peppering your script with print statements to see when the corruption happens. print "$server_list_hash{$server}\n";

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: can't use string as an array ref
by GotToBTru (Prior) on Mar 26, 2015 at 14:33 UTC

    Can you honestly expect an informed answer to this question without providing any information on what %server_list_hash contains? This is what Data::Dumper is for, or use the debugger to inspect the contents of variables just before the point of unexpected behavior. The former has the advantage that you could easily include that information in your post.

    Dum Spiro Spero

Log In?
Username:
Password:

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

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

    No recent polls found