Wiggins,
That works ok but still doesn't sort my keys correctly
Even if I use sort like so doesn't work. Like what I
have below:
foreach my $OS (sort keys(%commands)) {
while (my ($key, $command) = each (%{$commands{$OS}})) {
print "$key running $command\n";
}
}
Just wondering what if I add a number in my hash to indicate
Order. Maybe something like this:
%commands = ('sol'=>{'hostname' => [1,'uname -n'],
'os' => [2,'uname -s'],
'osver' => [3,'uname -r'],
'osrel' => [4,'cat /etc/release | awk \'{print
$3}\''],
'srvtype' => [5,'uname -p'],
'srvmodel' => [6,'uname -i | cut -f2 -d ","'],
'memory' => [7,'prtconf | grep Memory | awk
\'{print $3}\''],
'cpu' => [8,'psrinfo | awk \'{print $1}\' |
wc -l']}
);
That way I can index the file and use sort routine to do this:
foreach my $OS (sort {lc($a) <=> lc($b)} keys(%commands)) {
while (my ($key, $command) = each (%{$commands{$OS}})) {
print "$key running $command\n";
}
}
Maybe that might work but then how would I extract my commands.
Any thoughts?
Phillip
-----Original Message-----
From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED]
Sent: Friday, October 31, 2003 3:24 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: Hash Issue
[EMAIL PROTECTED] wrote:
> Wiggins,
>
> I tried that suggestion you have and got the
> The following message:
>
> C:\Perl\Accenture>perl test2.pl
> Can't use string ("sol") as a HASH ref while "strict refs" in use at
> test2.pl line 59.
>
> That is using your suggestion:
>
> foreach my $OS (keys(%commands)) {
> while (my ($key, $command) = each (%$OS)) {
> print "$key running $command\n";
> }
> }
Sorry I should test my own suggestions ;)...
That should have been:
foreach my $OS (keys(%commands)) {
while (my ($key, $command) = each (%{$commands{$OS}})) {
print "$key running $command\n";
}
}
http://danconia.org
> -----Original Message-----
> From: Wiggins d Anconia [mailto:[EMAIL PROTECTED]
> Sent: Friday, October 31, 2003 12:33 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: Hash Issue
>
>
>
>
>>Hi,
>>
>>
>>
>> I've been playing around with the Tie::IxHash module.
>>
>>
>>
>> Here is my part of my code:
>>
>
>
> It appears to be lacking, use strict and use warnings these will help
> you track down on your own your errors....
>
>
>>
>>
>>tie my %commands, "Tie::IxHash";
>>
>>
>>
>>%commands = ('sol'=>{'hostname' =>'uname -n',
>>
>> 'os' =>'unamed -s',
>>
>> 'over' =>'uname -r',
>>
>> 'osrel' =>'cat /etc/release | awk
>>\'{print $3}\'',
>>
>> 'srvtype' =>'uname -p',
>>
>> 'srvmodel' =>'uname -i | cut -f2 -d ","',
>>
>> 'memory' =>'prtconf | grep Memory | awk
>>\'{print $3}\'',
>>
>> 'cpu' =>'psrinfo | awk \'{print
>>$1}\' | wc -l'}
>>
>> );
>>
>>
>
> I haven't used the tied hash much, but assuming it works as a regular
> has as it should...
>
>
>>
>>
>>foreach $OS (keys %commands){
>>
>
>
> Right here $OS contains a hash reference to the inner hash for the
> particular operating system...
>
>
>> print "OS: $OS \n";
>>
>> while (( $OS, $CMD ) = each %commands ) {
>>
>
>
> Right here you are eaching over %commands inside a foreach on keys which
> is probably not what you want, and is probably doing screwy things, like
> resetting the position indicator of the hash.... And you are also
> clobbering your preset $OS which means you can no longer access that
> particular hash to loop over.
>
>
>> print "$OS Commands are $items .\n"; *****
>>
>
>
> Where did $items come from in the first place.
>
>
>> }
>>
>>}
>>
>>
>>
>>If I change the $items variable in the print statement where the
>
> asterisk is
>
>>to $CMD I get nothing but a hex value output. Has anyone have a suggestion
>>what I'm doing wrong.
>>
>
>
> How about:
>
> foreach my $OS (keys(%commands)) {
> while (my ($key, $command) = each (%$OS)) {
> print "$key running $command\n";
> }
> }
>
> I was thinking that your use of the tied hash was to get the commands to
> run in order rather than the OS's (maybe I missed part of this
> discussion) if that is the case the inner hashes must be the tied
> variants, rather than the outer.....
>
> perldoc perllol
> perldoc perldsc
> perldoc perlreftut
> perldoc perlref
>
> http://danconia.org
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]