Loading ...
Sorry, an error occurred while loading the content.

separating elements into individual variables.

Expand Messages
  • zylogue
    Here is a piece of code I was given from IRC: /begin use Data::Dumper ; @buf=`vmstat`; @h{split ,$buf[1]}=split ,$buf[2]; print Dumper %h; /end Here is the
    Message 1 of 2 , Jun 4, 2002
    • 0 Attachment
      Here is a piece of code I was given from IRC:

      /begin

      use Data::Dumper ;

      @buf=`vmstat`;
      @h{split" ",$buf[1]}=split" ",$buf[2];
      print Dumper\%h;

      /end

      Here is the output:

      /begin

      $VAR1 = {
      'cache' => '157700',
      'swpd' => '3488',
      'bi' => '1',
      'us' => '10',
      'si' => '0',
      'b' => '0',
      'r' => '2',
      'sy' => '2',
      'in' => '7',
      'buff' => '47252',
      'bo' => '9',
      'w' => '0',
      'free' => '66288',
      'id' => '3',
      'so' => '0',
      'cs' => '10'
      };

      /end

      I do not have the vocabulary or understanding to know what this code
      has done. I am wanting to take data from the vmstat command, average
      5 seconds worth of data per 'column', set each averaged column to its
      own variable, and then put specific varviable groupings into .rrd
      files for graphing system performance.

      COuld someone please explain this code to me, so I can start trying
      to develop the next step of this script?

      Thanks
    • b_harnish
      ... ... # @buf=`vmstat`; Takes all the output from the vmstat command, and places it into the @buf array, each line of output = 1 element in the array.
      Message 2 of 2 , Jun 5, 2002
      • 0 Attachment
        --- In perl-beginner@y..., "zylogue" <dcurry@c...> wrote:
        > Here is a piece of code I was given from IRC:
        >
        > /begin
        >
        > use Data::Dumper ;
        >
        > @buf=`vmstat`;
        > @h{split" ",$buf[1]}=split" ",$buf[2];
        > print Dumper\%h;
        >
        > /end
        <snip>
        > COuld someone please explain this code to me, so I can start trying
        > to develop the next step of this script?

        # @buf=`vmstat`;
        Takes all the output from the vmstat command, and places it into the @buf array, each line of output = 1 element in the array. (Technically, it splits the output on '$/'.)

        # @h{split" ",$buf[1]}=split" ",$buf[2];
        We can break this into seperate statements to clarify:
        # @a1 = split" ",$buf[1];
        Takes element 1 (2nd element (line of o/p)) and splits it into an array on space. Ie: 'a b cd e f' would have 5 elements 'a','b','cd','e','f'.
        # @a2 = split" ",$buf[2];
        Same thing, but with element 2 (3rd element (line of o/p))
        # @h{@a1} = @a2;
        Takes the hash %h, temporarily (almost) turns it into an array using @a1 as the index's. Takes the values from @a2 and puts them into the temporary (almost) array.

        # print Dumper \%h;
        Dumps the array to STDOUT.

        - Brian
      Your message has been successfully submitted and would be delivered to recipients shortly.