Beckett Richard-qswi266 wrote:

Right. So, if I was writing a test tool to perform pings, using an array for
the variables, I could have... (obviously very cut-down)

@vars = ("cheese.street.town.com", 0, 5, 1024, 100, "ping.xls")
# i.e. hostname, interval, timeout, size, number-of-pings, save-results-as
...
$ping = Net::Ping -> new ('icmp', $vars[2], $vars[3]);
for ($loop = 1; $loop <= $vars[4]; $loop++) {
   $ping -> ping ($vars[0], $vars[2])
   usleep ($vars[1] * 1000);
}
...
#close results spreadsheet
$vars[5] -> close (0);
That would be a silly use of an array, your example below would
be much more reasonable.

But I _would_ have written it using variables like this:
$host = ...;
$interval = 0;
$timeout = 0;
$size = 1024;
etc.
So I could have the lines:
$ping = Net::Ping -> new ('icmp', $timeout, $size);
$ping -> ping ($host, $timeout);

Also, if I had a GUI to enter the variables, I would have labels and entry
boxes called $host_label, $timeout_label..., $host_entry, $timeout_entry...
AND I would want to save the variables, so that they could be auto-loaded
next time the script is run, so I'd want an easy way to get and insert the
value of the variables into the entry boxes.

Is this where a hash comes in? This is where I start to lose it.

Could I have...

# If the hash has been saved using Storable...
$ref = retrieve 'saved.dat' if -e 'saved.dat';

if ($ref) {
   print "Saved values found.\n";
   %vars = %$ref;
} else {
   print "Using default values.\n";
   %vars = (
   host => ["cheese.street.town.com", "Hostname: ", undef],
   interval => [0, "Interval (ms): ", undef],
   timeout => [5, "Timeout (sec): ", undef],
   ...);

# i.e. hostname value, label, entry widget,
       interval value, label, entry widget,
       ...
}

Then, when I create the labels and entry boxes... (BUT, how would I control
the order they come out of the hash?!!)

foreach (%vars) {

   my $label = $frame -> Label (-text => $vars{$_[1]}) -> pack ();
   my $entry = $other -> Entry () -> pack();
   $entry -> insert ("end", $vars{$_);
   $vars{$_[2]} = $entry; # stores the entry in the hash
}
...

Before testing, must get the values from the entry boxes...

foreach (%vars) {
   $vars{$_[0]} = $vars{$_[2]} -> get;
}

Finally I can test...
$ping = Net::Ping -> new ('icmp', $vars{timeout}, $vars{size});
$ping -> ping ($vars{host}, $vars{timeout});
...

If the user changes the values in the entry boxes, then reloads the saved
settings...

foreach (%vars) {
   $vars{$_[2]} -> delete (0, 'end');
   $vars{$_[2]} -> insert {'end', $vars{$_});
}

Sorry this is so long, (and probably unintelligable), but that's the sort of
thing I'm trying to do, which is why I'm trying to use hashes (and getting
myself into a complete knot!).
In addition to the other response you received on this (hash of hashes etc),
consider using split and join for a lot of these types of apps.

# replace ',' with any delim that's suitable:

my $obj = join ',', "cheese.street.town.com", 0, 5, 1024, 100, "ping.xls";

...  Do stuff (maybe store away the data and later get your data back
     from stores with:

my ($host, $int, $timeout, $size, $numpings, $saveas) = split /,/, $obj;

Which is basically another way to do storable kinda things with a flat file.

--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
 (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to