当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Using Perl Hash to populate a drop-down list?

问题描述:

Newbie Alert I want to basically read in a few files to get some data out of it. I want to parse this data into a hash table to eventually generate a bar graph.

Here is the type of file, I am reading in. All the files are much more bigger and have many more entries:

> Name CPU Time CPU Load Percentage

>

> -----------------------------------------

>

> System 7962 3.00%

>

> Adobe Photoshop 6783 0.19%

>

> MSN Messenger 4490 0.01%

>

> Google Chrome 8783 0.02%

>

> Idle 120 94.00%

>

> =========================================

Here's my relevant code for Perl so far:

#!/usr/bin/perl

use warnings;

use Data::Dumper;

#Looping through the end of the file

while (<DATA>)

{

#Selecting only the CPU Processes

if (/(.*?)\s+\d+\s+(\d+\.\d+)\%/){

#Populating the hash with only Process name and % (eg: Idle 94.00%)

$cpu_util{ $1 } = $2;

}

}

#Assigning the axis labels to be pushed onto the graph

@xvalues = keys %cpu_util;

@yvalues = values %cpu_util;

}


Now, I am able to successfully prase data from multiple files and be able to display a single graph. However, what I'd like to be able to do is, inaddition to displaying just one graph. Provide the user to select which process their interested in and then create a line graph of those points.

However, I'm not completely sure how to go about this. I know all my proccesses are stored in my hash via key/value pairs. So ("Idle -> 94%") etc. What I'd like to end up doing is show a user, a drop down box ... and it lists all the keys in the hash. When the user selected a specific key, it generates a GD::Graph for him. I guess, I will generate the graphs for each key before hand itself and save them as images. Selecting a specific option merely links them to the right image ...

I've been doing some reading online regarding implementing a drop down list using CGI but I haven't gotten too far. I'm thinking of creating another HTML page with the drop-list, but I'm not sure how to get the values to populate dynamicly from the hash stored in Perl ..

网友答案:

First, look up Data::Dumper and Data::Dump. These will help you whenever you're working with data structures.

To do a line graph you need time-series data. It sounds like you have multiple files, each a snapshot at a given point in time. Right now you're just summing them, instead you need to load all the data. So you could store it something like:

foreach my $file (@files) {
    my $time = get_time_from_file($file); # regexp on name, whatever
    # ...
    $cpu_util{$1}{$time} = $2; # or $cpu_util{$time}{$1}
    #...
}

You'll then have both time and program in your data structure, so you can do graphing over time. This will, of course, use more memory (but I'm guessing that isn't really an issue unless you have quite a few of these files).

edit 1

Once you have all the data loaded into memory, you can slice it however you want. So you can graph a single program over time or whatever. CGI.pm is definitely "oldschool" (it's how we wrote Perl CGI in the mid-90's), but it should work fine for making your select box. You can see what is selected using its param method:

my $selected = $CGI->param('selectbox_name');
if (defined $selected) {
   # code to display graph
} else {
   # code to display menu
}

You could pregenerate all graphs, whether that's a good idea or not really depends on how manny graphs it'd be, how much traffic each one gets, what the response time requirements are, etc.

(You may also want to look into things like HTML::Template, Template Toolkit, Mason, CGI::Application, and Catalyst. Also, instead of repeatedly processing the files, you may want to store the results somewhere. SQLite and rrdtool come to mind. But learning all of that at once will probably be quite overwhelming.)

分享给朋友:
您可能感兴趣的文章:
随机阅读: