Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Is this concise enough? (code)

by deprecated (Priest)
on Jun 24, 2001 at 01:30 UTC ( #91008=perlquestion: print w/ replies, xml ) Need Help??
deprecated has asked for the wisdom of the Perl Monks concerning the following question:

Yes, I am using perl where I could just be using the shell. In this case, I am using perl because it is a little less processor intense (fewer processes forked), and a lot clearer from a readability point of view.

The application is a wrapper around a tape stacker. I want to read in six tapes from a stacker, in sequence, using the command `mtx`. I'm reading its output, here:

slot_1 Full slot_2 Full slot_3 Empty slot_4 Full slot_5 Full slot_6 Full Drive: tape 3 loaded

I can then figure out which tape is in the drive and which ones need to be done. Anyhow, the code that I have created to parse that information is here:

# status in this case is the output of mtx, above... my @fields = split m[\n|\s+], $status, 13; my $loaded = pop @fields; my %drives = @fields;
This is pretty cool, but I dont get a feeling of satisfaction from it. What would be a more concise yet still clear way to do this?

thanks
brother dep

--
Laziness, Impatience, Hubris, and Generosity.

Comment on Is this concise enough? (code)
Select or Download Code
Re: Is this concise enough? (code)
by particle (Vicar) on Jun 24, 2001 at 04:31 UTC
    well this looked fun so i gave it a shot:
    # using your output in var $status my (%drives, $loaded); # remove last line from $status and assign to $loaded # of course, this assumes nice, predictable output, since # it's simply looking for the word Drive: $status =~ s/\n(Drive:.+)$// && ($loaded = $1); # create your hash with the remainder of $status %drives = split( m[\n|\s+], $status, 12 );
    i think your way is clearer, but it was an interesting diversion. at least now if i see some better examples, i'll have a good idea of what they're doing.

    i tried, but i couldn't find any functions with side effects that could be put to good use here.

    ~Particle

(tye)Re: Is this concise enough? (code)
by tye (Cardinal) on Jun 24, 2001 at 10:20 UTC
    my( $loaded, %drives )= ( split m[\s+], $status, 13 )[12,0..11];
            - tye (but my friends call me "Tye")
Re: Is this concise enough? (code)
by chipmunk (Parson) on Jun 24, 2001 at 18:08 UTC
    The regular expression you've used in the split is a bit odd. \s+ matches \n, so the regex is mostly redundant. The one difference can be seen with these two snippets:
    my @fields = split ' ', "abc\n def"; my @fields = split /\n|\s+/, "abc\n def";
    The first splits on "\n ", producing ('abc', 'def'), while the second splits on "\n" and " ", producing ('abc', '', 'def'). In other words, split /\n|\s+/ will give you null strings where there's a newline followed by whitespace (including blank lines).

    One other point; your code depends on having exactly six slots. You might consider writing it to work with any number of slots.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://91008]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-11-27 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls