Perl hashes "Can't use subscript on private hash"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl hashes "Can't use subscript on private hash"
# 1  
Old 07-23-2008
Question Perl hashes "Can't use subscript on private hash"

This is driving me mad, where am I going wrong?

The relevant segment of code:
Code:
sub getndsybcons {
        my @servers=@{$_[0]};
        my @sybservers=@{$_[1]};
        my %results;
        foreach my $server(@servers) {
                my $biggestsyb;
                my $biggestsybval=0;
                foreach my $sybserver(@sybservers) {
                        open(NETSTAT,"$config{'su'} - $config{'user'} -c \'ssh $server \"netstat -a\"\' | grep $sybserver | wc -l|") || return;
                        while(<NETSTAT>) { $tempval=$_ }
                        if ($tempval > $biggestsybval) {
                                $biggestsybval=$tempval;
                                $biggestsyb=$sybserver;
                        }
                }
                if ($biggestsybval > 5) {
                        $results{$server}=$biggestsyb;
                } else {
                        $results{$server}="";
                }
        }
        return %results;
}

The error:
Code:
Can't use subscript on private hash at ./ib-lib.pl line 22, near "$server}"
(Did you mean $ or @ instead of %?)
Can't use subscript on private hash at ./ib-lib.pl line 24, near "$server}"
(Did you mean $ or @ instead of %?)

Lines 22 and 24 are bolded.

This code is for a webmin module to control an application's various components.
This bit of perl is part of a library of functions called by the main GUI interface code. Webmin provides the %config structure.

I've never been that great with hashes at the best of times but this one's got me really boggled.

It feels like I've got my variable declarations wrong (I'm attempting to minimise global variables that are not used as constants (eg %config), but have been finding this rather fiddly as I'm _very_ rusty at "real" coding Smilie ) but I don't seem to be getting anywhere playing round with it...

Can someone cast some light on this for me?
# 2  
Old 07-23-2008
I can't repro that. Do you have a global variable %results or a subroutine results elsewhere in your script? (Still can't repro with either of those, but I can't come up with anything better ...) Or just a missing closing brace somewhere -- those can trigger quite misleading error messages.

What's the point of an open("long pipeline |") loop if all you want is a single value? I'd use backticks for that. Also the return on error without a proper error message looks user-hostile and fragile. (Of course if the error checking is deferred to the caller then I suppose it's as it should be.)

Last edited by era; 07-23-2008 at 02:15 AM.. Reason: Suggest use backticks
# 3  
Old 07-23-2008
hmm.... very weird. I think you should print the value or $server while the script runs, might help debug the problem. Just looking at the code I don't see any problem with syntax.
# 4  
Old 07-24-2008
Question

Quote:
Originally Posted by era
What's the point of an open("long pipeline |") loop if all you want is a single value? I'd use backticks for that.
That's a really good point, it used to be a more complex regex Smilie

I've since got it running, by removing the 'my' keyword where it's setting $server if the foreach loop:
Code:
my %results;
        foreach my $server(@servers) {
                my $biggestsyb;
                my $biggestsybval=0;

I won't pretend to understand why this worked though...
# 5  
Old 07-24-2008
Me either, I can't see why that would make a difference. Is $server scoped to that block of code elsewhere in the script? What is the value of $server with "my" and without "my"?
# 6  
Old 07-24-2008
Quote:
Originally Posted by KevinADC
Me either, I can't see why that would make a difference. Is $server scoped to that block of code elsewhere in the script? What is the value of $server with "my" and without "my"?
If I leave the 'my' in, it won't execute at all, if I leave it out, $server has the correct value. Very hard to debug....
# 7  
Old 07-24-2008
That just makes no sense from the code that you posted. What are the values in @servers?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

3. Shell Programming and Scripting

Perl: Printing null hash values as a " "?

I'm filling in a table of values for grades. I decided to go with reading into a hash from the files but I'm coming up with an error when printing a value that does not exist. I need to know if I can on-the-fly print a space (" ") or blank in place of the grade. Here's what the output should... (2 Replies)
Discussion started by: D2K
2 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

perl: dereferencing a hash of hashes

Hi there, I am trying to dereference my hash of hashes but post dereferencing, it seems to lose its structure I am using Data::dumper to help me anaylise. This is the code im using to build the HoH, (data comes from a file). I have also performed a Dumper on the data structure before and after... (1 Reply)
Discussion started by: rethink
1 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

perl hash of hashes from database

hi there, I have some database output that looks like this SELECT nic_name,nic_duplex,nic_speed,nic_ip FROM network_table WHERE hostname = "server1" result is this (ive delimited with a pipe for ease of reading) bge0|full|1000|10.32.100.1 bge1|full|1000|11.12.101.7 ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
Login or Register to Ask a Question