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

Monks, this is frustrating. Why doesnt the following work? I get an undefined variable returned.
in getClusterHostCount$VAR1 = '/saconfig/current/cluster1'; $VAR2 = [ 'localhost', 'eh050su2' ]; sub getClusterHostCount { print "******************entering getclusterhost count\n"; my $hostcount = ( @{$ClusterHostList{ $clusterID }} ) ; print "count = $hostcount\n"; print "in getClusterHostCount", Dumper (%ClusterHostList); return $hostcount; }

Replies are listed 'Best First'.
Re: Number of Elements in HoL
by perrin (Chancellor) on Aug 22, 2001 at 17:58 UTC
    It looks like you're putting that array ref in list context when you want scalar context. Try this:
    my $hostcount = scalar(@{$ClusterHostList{ $clusterID }});
    Technically, I don't think you need that "scalar()", but it's good documentation of what you're doing.
Re: Number of Elements in HoL
by MZSanford (Curate) on Aug 22, 2001 at 17:51 UTC
    I don't see $clusterID getting set, if that is not just acopy/paste error, i think thats the cause ... i used the folowing for a test :
    $ClusterHostList{'/saconfig/current/cluster1'} = ['localhost','eh050su +2']; &getClusterHostCount('/saconfig/current/cluster1'); sub getClusterHostCount { print "******************entering getclusterhost count\n"; my $clusterID = shift; my $hostcount = scalar @{ $ClusterHostList{$clusterID} } ; print "count = $hostcount\n"; return $hostcount; }
    .. and suvery says ...
    ******************entering getclusterhost count count = 2

    Update : /me being stupid. It was set (thanks Tuna), so my other fix (scalar) was what did it.
    can't sleep clowns will eat me
    -- MZSanford