Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^5: How to add columns with new row name using perl from mysql query?

by huck (Parson)
on Apr 05, 2017 at 13:03 UTC ( #1187108=note: print w/replies, xml ) Need Help??


in reply to Re^4: How to add columns with new row name using perl from mysql query?
in thread How to add columns with new row name using perl from mysql query?

back to your pie charts now huh?

It might help if you understood what your var data_run= statment was supposed to look like. If i remember correctly it is an array of arrays. The interior arrays contain a label and a value.

var data_run=[ ["label a",1],["label b",2],["label_c",99]];
use strict; use warnings; use DBI; my $storagefile='finddata'; my $DBH = DBI->connect( "dbi:SQLite:dbname=".$storagefile ) || die "Ca +nnot connect: $storagefile $DBI::errstr"; my $sql = 'SELECT queue_name,jobs_pend,jobs_run FROM queues ORDER BY jobs_run DESC'; my $sth = $DBH->prepare( $sql ); $sth->execute(); # input my %table = (); my $recno = 0; my @top = (); while (my ($name,$pend,$run) = $sth->fetchrow_array){ my $key = ($recno++ < 5) ? $name : 'other' ; push @top,$key unless (defined ($top[-1]) && $top[-1] eq 'other'); $table{$key}{'pend'} += $pend; $table{$key}{'run'} += $run; } #So first you need to make your interior arrays my @iarray_run; for my $key (@top){ push @iarray_run,'["'.$key.'",'.$table{$key}{'run'}.']'; } # then you join them and assign them my $orun=join(',',@iarray_run); print "var data_run=[$orun];\n";
Result
var data_run=[["adice_short",192],["ncsim_short",84],["ncsim_long",78] +,["adice_long",39],["normal",30],["other",34]];
They must be hard up for programmers where you work. It takes you days to produce something that anyone competent would do in an hour.

Replies are listed 'Best First'.
Re^6: How to add columns with new row name using perl from mysql query?
by Anonymous Monk on Apr 05, 2017 at 13:12 UTC
    ' It takes you days to produce something that anyone competent would do in an hour.'. Wrong. They post here, you do their job and complain, goto 10. Keep it up
        missing your own point. finddata/perlanswers has you jumping through hoops to do their job, it's the same guy.
      This output i got from previous output itself the thing i tried it to push into some piechart format so i had showed my code with following format .Thats why i had posted my code with the following format while am posting my question.
      For example:Just for reference i showed you this code
      while(my @row_array=$sth->fetchrow_array) { if ($tmp == 0) { $var_data_running .= "\[\"$row_array[0] \($row_array[2]\)\",$r +ow_array[2]\]"; $var_data_pending .= "\[\"$row_array[0] \($row_array[1]\)\",$r +ow_array[1]\]"; $tmp++; } else { $var_data_running .= ",\[\"$row_array[0] \($row_array[2]\)\",$ +row_array[2]\]"; $var_data_pending .= ",\[\"$row_array[0] \($row_array[1]\)\",$ +row_array[1]\]"; } }

      In your recent code you had used join then you are printing those variables
      .Here the variable prints properly.To include it into some chart format i had reformat the following part of code as like the above one which as follows
      my $orun=join('',@iarray_run); my $opend=join('',@iarray_pend);

        If you were paying attention you would see that even your code had commas between the interior arrays.

        $var_data_running .= ",\[\"$row_array[0] \($row_array[2]\)\",$row_arr +ay[2]\]";
        That is why
        my $orun=join(',',@iarray_run); print "var data_run=[$orun];\n";
        is correct.
        use strict; use warnings; use DBI; my $storagefile='finddata'; my $DBH = DBI->connect( "dbi:SQLite:dbname=".$storagefile ) || die "Ca +nnot connect: $storagefile $DBI::errstr"; my $sql = 'SELECT queue_name,jobs_pend,jobs_run FROM queues ORDER BY jobs_run DESC'; my $sth = $DBH->prepare( $sql ); $sth->execute(); # input my %table = (); my $recno = 0; my @top = (); while (my ($name,$pend,$run) = $sth->fetchrow_array){ my $key = ($recno++ < 5) ? $name : 'other' ; push @top,$key unless (defined ($top[-1]) && $top[-1] eq 'other'); $table{$key}{'pend'} += $pend; $table{$key}{'run'} += $run; } #So first you need to make your interior arrays my @iarray_run; for my $key (@top){ push @iarray_run,'["'.$key.'",'.$table{$key}{'run'}.']'; } # then you join them and assign them my $orun=join(',',@iarray_run); print "var data_run=[$orun];\n"; my $orun2=join('',@iarray_run); print "wrong-var data_run=[$orun2];\n"; print "\n"; use JSON 'decode_json'; my $frjson_data_run = decode_json( "[$orun2]" );
        Result
        var data_run=[["adice_short",192],["ncsim_short",84],["ncsim_long",78] +,["adice_long",39],["normal",30],["other",34]]; wrong-var data_run=[["adice_short",192]["ncsim_short",84]["ncsim_long" +,78]["adice_long",39]["normal",30]["other",34]]; , or ] expected while parsing array, at character offset 21 (before "" +ncsim_short",84]["n...") at 1187071a.pl line 38.
        Even poj's code at Re^5: How to add columns with new row name using perl from mysql query? via encode_json prints
        var data_run = [["adice_short (192)","192"],["ncsim_short (84)","84"], +["ncsim_long (78)","78"],["adice_long (39)","39"],["normal (30)","30" +],["other (34)","34"]]
        so i think you still need to pay much more attention to what you are doing.

        http://www.json.org/An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

        Just another day wasted i guess

        You owe The Oracle some funky characters. And still no proper grovel.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1187108]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-10-27 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (256 votes). Check out past polls.

    Notices?