Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Using the map function

by davido (Archbishop)
on Jul 11, 2013 at 00:08 UTC ( #1043598=note: print w/ replies, xml ) Need Help??


in reply to Using the map function

You're really just partitioning an array. Slices are handy, but first you have to find the pivot point.

use List::MoreUtils qw( first_index ); sub partition { my $regex = shift; my $part = first_index { $_ =~ $regex } @_; return [ @_[ 0 .. $part - 1 ] ], [ @_[ $part + 1 .. $#_ ] ]; } my @array = ( 'CPU Temp = 30', 'GFX Temp = 45', 'RAM Temp = 40', ' ', # Space (pivot). 'CPU Status = OK', 'GFX Status = OK', 'RAM Status = OK', ); my( $temp, $status ) = partition( qr/^\s*$/, @array ); { local $" = ", "; print "Temp: ( @{$temp} )\n"; print "Status: ( @{$status} )\n"; }

...produces...

Temp: ( CPU Temp = 30, GFX Temp = 45, RAM Temp = 40 ) Status: ( CPU Status = OK, GFX Status = OK, RAM Status = OK )

If you're reading these in from a file, you don't really need to hold the original list in an array. You can just push onto @temp until a blank (or all space) line is reached, and then start pushing onto @status. But your question stated the data is already in an array, so my code will handle that well.


Dave


Comment on Re: Using the map function
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (22)
As of 2015-07-29 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (265 votes), past polls