At 01:47 2002.02.13, you wrote:
>I have data like this.
>
>Computername:Description:Failed Pings
>
>At present I am putting this data into seperate arrays
>and using an index to get it out in order. Not an
>extremely efficient way to do this and I will need to
>add more fields later and the like.
>
>I have considered using a hash, but if I,  say use the
>keys function, and am using a multidimensional hash
>(i.e $hash => computername => description => failed pings) it will, I
>assume, return all the values as keys, whereas I only want the first layer
>(I.E Computername)
>then I can use it to referance the data with known keynames.
>
>How would I do this? Could you please provide some
>example code for me? Thanks a lot in advance..
>
>Regards,
>Lorne

Off hand, I see two ways for you:

Aray of Aray

Data looks like

   @data = ( [ "Computername1", "Description1", "Failed Pings 1" ],
             [ "Computername2", "Description2", "Failed Pings 2" ],
             [ "Computername2", "Description2", "Failed Pings 2" ] );

  foreach my $logentry (@data) {
    foreach my $element (@$logentry) {
        # do whatever
    }
    # $logentry[0] = "computername"
    # $logentry[1] = "description"
    # $logentry[2] = "failed pings"
  }

  # $data[0][0] = "Computername1"
  # $data[1][2] = "Failed Pings 2"

Aray of Hash

  @data = ( { name => "Computername1", desc => "Description1", 
              result => "Failed Pings 1" },
            { name => "Computername2", desc => "Description2", 
              result => "Failed Pings 2" },
            { name => "Computername3", desc => "Description3", 
              result => "Failed Pings 3" } );

  for my $logentry (@data) { # foreach and for are the same when used this way
    for my $key (sort keys %$logentry) {
      # Get all entry in alpha number (desc, name, result)
    }
    # $logentry{name} = "computername"
    # $logentry{desc} = "description"
    # $logentry{result} = "failed pings"
  }

  # $data[0]{name} = "Computername1"
  # $data[1]{result} = "Failed Pings 2"

It all depends on all you want to parse you data. With the aray of aray solution, you 
must take care of the order and will end always adding to the end without changing the 
order at all. With the aray of hash, the order of each line is not important and you 
have to specify the order yourself when you want to print your data.

Hope this helps.


----------------------------------------------------------
Éric Beaudoin               <mailto:[EMAIL PROTECTED]>


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

Reply via email to