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

Enkidu enkidu at cliffp.com
Sat Sep 4 00:46:32 CDT 2004


On Sat, 04 Sep 2004 16:05:55 +1200, you wrote:

>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 ]
>)
>
Another example of the power of REs. Another would be the analysis of
logfiles. These are almost always a fixed number of fields and can be
split up using a single RE. Imagine writing code to do it - several
lines long and lots of loops.....

Cheers,

Cliff



More information about the Wellington-pm mailing list