Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

parse a csv file and create array of arrays and then plot the data

by kamal (Sexton)
on Jul 20, 2010 at 10:39 UTC ( #850395=perlquestion: print w/replies, xml ) Need Help??
kamal has asked for the wisdom of the Perl Monks concerning the following question:

What i am trying to do, is to parse a CSV file and create array of arrays  so can use them in :

my $data = [     2, 3, 4, 5, 6, 7 ,     1, 4, 5, 2, 1, 5 ,     3, 6, 7, 5, 4, 3 , ]; replacing the individual array by $csvdata[n]

1 The CSV file is :

Date,Av,SEU,128,440,"1,024",Mixed,,rules profiling,snort,software version 7/14/2010,3.40,348,349.0,1254.9,3540.6,648.6,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 344,-1.22%,-0.02%,2.94%,0.79% 7/2/2010,3.40,344,353.3,1255.1,3439.4,643.5,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 343,0.37%,0.75%,-0.91%,-1.11% 6/30/2010,3.40,343,352.0,1245.8,3471.1,650.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 342,-0.26%,-0.91%,1.51%,-0.97% 6/24/2010,3.40,342,352.9,1257.2,3419.5,657.1,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 341,0.23%,0.50%,-1.34%,0.67%

2. i parse a csv file
#!/usr/bin/perl use strict; use warnings; use Tie::CSV_File; tie my @csvdata, 'Tie::CSV_File', 'perf.csv'; # print "Line 5, column 1: ", $csvdata[4][0] . "\n"; untie @csvdata;
2. get $csvdatan 3 thru 6 and plot them against time so that X Axis is time Y axis is MBps and the 4 series, namely 128,440,"1,024",Mixed, and plotted against time plot the data using Spreadsheet::WriteExcel Thanks, -Kamal.

Replies are listed 'Best First'.
Re: parse a csv file and create array of arrays and then plot the data
by ambrus (Abbot) on Jul 20, 2010 at 11:54 UTC

    Assume the csv file is called a.csv, then preprocess it a bit with this command:

    <a.csv tr , \\t | tail -n+2 >a.tsv

    Now start gnuplot, and enter the following commands:

    set timefmt "%m/%d/%Y" + set xdata time set key outside below plot \ "a.tsv" using 1:4 title "128", \ "a.tsv" using 1:5 title "440", \ "a.tsv" using 1:6 title "1024", \ "a.tsv" using 1:7 title "Mixed"

    Feel free to change the commands if you want to change the formatting, or use the set terminal and set output commands to save the plot as an image.

    See also Plot a spiral with gnuplot about the general idea of using gnuplot in perl, and Re: Draw chart as an example that uses times as one coordinate.

Re: parse a csv file and create array of arrays and then plot the data
by ww (Archbishop) on Jul 20, 2010 at 11:58 UTC
    If your question is "Will you write this for me?" the answer is "No."

    If you need help on some particular part of your code, then show what you've tried, and we'll be glad to help.

    That's just the way the Monastery works. See On asking for help and How do I post a question effectively?.

      :) i dont want anyone to write it for me. Even though looking at what i wrote looks that way. My Question is: In the code, i have the following as sample data
      my $data = [ [ 0,500,1000,1500,2000,2500,3000,3500,4000 ], [ 1000, 450, 550, 240, 120, 330 ], [ 3000, 650, 750, 555, 440, 550 ], ];
      I tried substituting the individual arrays in $data with: $column4 because the array reference $column4 is in a while loop so what i need is a hint of how to incorporate $column in $data
Re: parse a csv file and create array of arrays and then plot the data
by jethro (Monsignor) on Jul 20, 2010 at 11:11 UTC
    And what is your question ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://850395]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-06-27 06:39 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (600 votes). Check out past polls.