[Wellington-pm] Looking for an easier, clearer way to ...

Jacinta Richardson jarich at perltraining.com.au
Sat Sep 4 00:00:32 CDT 2004


Grant McLean wrote:
> A local Perl newbie sent me the following message directly.
> Let's show him what a helpful bunch of mongers we are ...
> 
> 
> -- Original Message Follows --
> 
> I'm sure there must be an easier, clearer way to do this;
> 
> my $diskdescr =
>       `snmpwalk $hostname -c $communityname -Oqs -v1 extOutput`;
> chomp $diskdescr;
> 
> # $diskdescr looks like this;
> # extOutput.1 8,0,148646203,16523952,1112455874,132122251,2116450096:8,1,638055135,362646180,795647676,275408955,3348765608
> # $diskdescr has extOutput.1  at the beginning, I need to drop that
> my ($rubbish,$rest) = split ' ', $diskdescr;
> 
> my @diskioentries = split ':', $rest;
> 
> Suggestions welcome!

First of all, this code appears to do what is required and is easy to
read and understand (at least to me), so thumbs up to that.  I can offer
alternative ways, to solve the problem but I can't say they'll be easier
or clearer.

The original code is:

          my $diskdescr = `result of some command`;
          chomp $diskdescr;

          my ($rubbish,$rest) = split ' ', $diskdescr;
          my @diskioentries = split ':', $rest;

This could be rewritten:

          my $diskdescr = `result of some command`;
          chomp $diskdescr;

          # Throw away first item, as rubbish, and keep the rest
          my $useful = (split ' ', $diskdescr, 2)[1];
          my @diskioentries = split ':', $useful;

This could be condensed to:

          my $diskdescr = `result of some command`;
          chomp $diskdescr;

          # Throw away first item, as rubbish, split rest of fields up
          my @diskioentries = split ':', ((split ' ', $diskdescr, 2)[1]);

But I'm not sure if that passes the requirements.  ;)
You could also use a regular expression:

          my $diskdescr = `result of some command`;

          # Split disk entries up on colons
          my @diskioentries = ($diskdescr =~ m/([\d,]+)(?::|$)/g);

Whether or not that's easier or clearer though... I can't say, it
depends on your familiarity with regexps.  If you need/want an
explanation of the regexp, feel free to ask.

All the best,

     Jacinta

-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |





More information about the Wellington-pm mailing list