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
|
|---|