Pergunta

I was trying to plot the stat using an external command, and the output of the external command is of two lines, something like

insert  query update delete getmore command flushes mapped  vsize
*0    961     *0     *0       0     4|0       0  42.2g  85.2g

I was trying to get this to a hash, so I can later call keys insert/query will give corresponding values 0/961 I read each line into an array, as

foreach my $line (@QPS_RAW){
    chomp $line;
    my @STATS_RAW=split("\n ", $line);
    push (@STATS, @STATS_RAW);
    print Dumper @STATS;
}

But I have no idea how to push each element of the first line into hash keys and each element of second line to values. Any pointers is greatly appreciated.

Foi útil?

Solução

Use a hash slice:

#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my @names  = split ' ', <>;
my @values = split ' ', <>;

my %hash;
@hash{@names} = @values;
print Dumper \%hash;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top