Wiggins,

   Thanks that certainly did the trick. Thank you and
   everyone else on this list for teaching me hash and
   providing help.

Phillip 
   
-----Original Message-----
From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 31, 2003 7:42 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: Hash Issue

[EMAIL PROTECTED] wrote:
> 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)) {

The above sorts the OS's not the commands...

>     while (my ($key, $command) = each (%{$commands{$OS}})) {

To get your commands sorted you would need to do it in the above at 
which point you are probably better off doing a foreach on the keys of 
the command list and applying your sort, see below....

>        print "$key running $command\n";
>     }
> }
> 
> Maybe that might work but then how would I extract my commands.
> Any thoughts?
> 

Something similar will work, but you will need to index into the hash of 
hashes of arrays to get to your number, which would be one more step of 
indirection that you wouldn't need to do if you tacked your number onto 
the command key instead, if this is possible, so you end up with 
something like:

#!/usr/bin/perl
use strict;
use warnings;
 

my %commands = ('sol'=>{ '1-hostname'  => 'uname -n',
                          '2-os'        => 'uname -s',
                          '3-osver'     => 'uname -r',
                          '4-osrel'     => 'cat /etc/release | awk 
\'{print $3}\'',
                          '5-srvtype'   => 'uname -p',
                          '6-srvmodel'  => 'uname -i | cut -f2 -d ","',
                          '7-memory'    => 'prtconf | grep Memory | awk 
\'{print $3}\'',
                          '8-cpu'       => 'psrinfo | awk \'{print $1}\' 
| wc -l',
                        },
                 'lin'=>{ '1-hostname'  => 'uname -n',
                          '2-os'        => 'uname -s',
                          '3-osver'     => 'uname -r',
                          '4-osrel'     => 'cat /etc/release | awk 
\'{print $3}\'',
                          '5-srvtype'   => 'uname -p',
                          '6-srvmodel'  => 'uname -i | cut -f2 -d ","',
                          '7-memory'    => 'prtconf | grep Memory | awk 
\'{print $3}\'',
                          '8-cpu'       => 'psrinfo | awk \'{print $1}\' 
| wc -l',
                        },
 

                );

foreach my $OS (sort keys(%commands)) {
     # the above sorts the os'es.
     print "\nOS: $OS\n";
 

     foreach my $key (sort keys(%{$commands{$OS}})) {
         # the above sorts the commands by their key name,
         # then we strip the sort order digit off with a regex
         # to get original key
 

         my ($step) = ($key =~ m/\d-(.*)/);
         print "\t$step: " . $commands{$OS}->{$key} . "\n";
     }
}

If the above isn't an option, or isn't desired for some reason, etc. 
below works with the structure you proposed:


#!/usr/bin/perl
use strict;
use warnings;
 

my %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', ],
                        },
                 'lin'=>{ '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', ],
                        },
 

 

                );

foreach my $OS (sort keys(%commands)) {
     # the above sorts the os'es.
     print "\nOS: $OS\n";
 

     foreach my $key (sort { $commands{$OS}->{$a}->[0]
                               <=>
                             $commands{$OS}->{$b}->[0]
                           } keys(%{$commands{$OS}})) {
 

         # the above sorts the commands by the first index
         # value of the array stored with the key
 

         print "\t$key: " . $commands{$OS}->{$key}->[1] . "\n";
     }
}
 

References and complex data structures like this can take a bit of 
getting used to, once it clicks it should make a lot of sense. I can't 
stress enough how much help the perldocs I listed earlier can be as well 
as using Data::Dumper to help you see what your structures look like.

Does this get it?

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to