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

Grant McLean grant at mclean.net.nz
Fri Sep 3 23:05:55 CDT 2004


Someone anonymous (let's call him 'Steve') wrote:
> 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;

'Steve'

It appears you want @diskioentries to end up containing two strings
of comma separated numbers.  If we assume you want the string of 
non-space characters either side of the colon then you could do this:

  my @diskioentries = $diskdescr =~ /(\S+):(\S+)/;

There's no need to chomp the newline off the end or strip the word 
off the beginning since the regex ignores them anyway.

If you wanted to go one step further and split the strings on commas
then you could do this:

my @diskioentries = map { [ split /,/ ] } $diskdescr =~ /(\S+):(\S+)/;

Which would have an equivalent result to:

my @diskioentries = (
  [ 8, 0, 148646203, 16523952, 1112455874, 132122251, 2116450096 ],
  [ 8, 1, 638055135, 362646180, 795647676, 275408955, 3348765608 ]
)

Whether that's useful to you or not is a whole other question :-)

Cheers
Grant



More information about the Wellington-pm mailing list