Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 meditating upon the Monastery: (14)
As of 2014-10-21 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (102 votes), past polls