in reply to I need to create a graph from a hash using GD else GD::Graph

You should probably use a module to read your XML, but if you really want to do it manually and get an array of arrays, you might want to follow the lines of this session under the Perl debugger:

DB<54> $c = '<data avgqu="0" avgrq="0" await="0" device="sdb" elapse +d_time="0" r="0" rmb="0" rrqm="0" svctm="0" util="0" w="0" wmb="0" wr +qm="0" />'; DB<55> push @AoA, [$c =~ /"([.\d]+)"/g]; DB<56> $c = '<data avgqu="0.00" avgrq="58.91" await="0.09" device="s +da" elapsed_time="1" r="0.00" rmb="0.00" rrqm="0.00" svctm="0.09" uti +l="0.05" w="5.50" wmb="324.00" wrqm="35.00" />'; DB<57> push @AoA, [$c =~ /"([.\d]+)"/g]; DB<58> $c = '<data avgqu="0.00" avgrq="58.91" await="0.09" device="s +da" elapsed_time="1" r="0.00" rmb="0.00" rrqm="0.00" svctm="0.09" uti +l="0.05" w="8.50" wmb="332.00" wrqm="27.00" />'; DB<59> push @AoA, [$c =~ /"([.\d]+)"/g];

This gives you the following structure:

DB<60> x @AoA 0 ARRAY(0x80432550) 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 1 ARRAY(0x80438020) 0 0.00 1 58.91 2 0.09 3 1 4 0.00 5 0.00 6 0.00 7 0.09 8 0.05 9 5.50 10 324.00 11 35.00 2 ARRAY(0x804381d0) 0 0.00 1 58.91 2 0.09 3 1 4 0.00 5 0.00 6 0.00 7 0.09 8 0.05 9 8.50 10 332.00 11 27.00

But I am not sure that this is what you are looking for, you haven't given enough details.

Replies are listed 'Best First'.
Re^2: I need to create a graph from a hash using GD else GD::Graph
by rahulruns (Scribe) on Sep 11, 2013 at 05:13 UTC

    I tried using XML modules but the parsed output gives only iostat as the tag and rest as the data which I need to remove. The original iostat data parsed to XML is in the form

    <iostat> <data avgqu="0" avgrq="0" await="0" device="sdb" elapsed_time="0" r= +"0" rmb="0" rrqm="0" svctm="0" util="0" w="0" wmb="0" wrqm="0" /> <data avgqu="0" avgrq="0" await="0" device="sda" elapsed_time="0" r= +"0" rmb="0" rrqm="0" svctm="0" util="0" w="0" wmb="0" wrqm="0" /> .........................(truncated) </iostat>
    Here I need to remove data and other special characters to get the data. From this data I need to draw a graph. For Drawing the Graph I need to use GD::Graph module which requires to use array of arrays. I am able to build a hash from the XML file which I have provided earlier but I am not able to convert it to array of arrays. Is it also possible to covert the hash to Excel, if yes how do I do that?

      The Test::Parser::Iostat has a data method which provides a hash, no need to parse the XML. This example plots a histogram of util against elapsed time for sda2 from a file created with iostat sda2 -x 1 10 > iostat.txt.

      #!/usr/bin/perl use strict; use Test::Parser::Iostat; use GD::Graph::bars; my $parser = new Test::Parser::Iostat(); open IN,'iostat.txt' or die "$!"; $parser->parse(\*IN) or die $parser->error(), "\n"; my $hash = $parser->data(); # create plot data my @plot=([],[]); for my $ar (@{$hash->{'iostat'}{'data'}}){ if ($ar->{'device'} eq 'sda2'){ push @plot[0],$ar->{'elapsed_time'}; push @plot[1],$ar->{'util'}; } } # create graph my $graph = GD::Graph::bars->new(400, 300); $graph->set( x_label => 'Elapsed time', y_label => 'Util', title => 'sda2 util', y_max_value => 100, ) or die $graph->error; my $gd = $graph->plot(\@plot) or die $graph->error; open(IMG, '>iostat.gif') or die $!; binmode IMG; print IMG $gd->gif;
      poj

      The hash that you extracted really contains only one linear array, which is probably not suitable for what you want to do. On the other hand, I provided you with a means to get an array of arrays, which is what you asked for in the first place, did you look at the AoA I obtained? Is it suitable for your purpose? As for GD::xxx, I do not know these modules nand can't help further.