The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 12:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 10:52 PM
how could i make a program mixed with many "|", "<" and ">" strugglingman High Level Programming 2 04-29-2006 05:11 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 01:15 AM
Adding "Private" to path name roadrunnerray UNIX for Dummies Questions & Answers 5 10-15-2004 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-22-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
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 ) but I don't seem to be getting anywhere playing round with it...

Can someone cast some light on this for me?
Reply With Quote
Forum Sponsor
  #2  
Old 07-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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-22-2008 at 10:15 PM. Reason: Suggest use backticks
Reply With Quote
  #3  
Old 07-22-2008
Registered User
 

Join Date: Jan 2008
Posts: 326
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.
Reply With Quote
  #4  
Old 07-23-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Question

Quote:
Originally Posted by era View Post
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

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...
Reply With Quote
  #5  
Old 07-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 326
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"?
Reply With Quote
  #6  
Old 07-23-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Quote:
Originally Posted by KevinADC View Post
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....
Reply With Quote
  #7  
Old 07-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 326
That just makes no sense from the code that you posted. What are the values in @servers?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
cgi, hash, perl, webmin

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0