使用 Perl Hash 填充下拉列表?

发布于 2024-10-01 18:43:33 字数 1418 浏览 2 评论 0原文

新手警报 我基本上想读入一些文件以从中获取一些数据。我想将这些数据解析为哈希表,最终生成条形图。

这是我正在读入的文件类型。所有文件都更大并且有更多条目:

> 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% 
> 
> =========================================

这是到目前为止我的 Perl 相关代码:

#!/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;  

      }

现在,我能够成功地从多个文件中读取数据,并且能够显示单个图表。然而,除了仅显示一张图表之外,我还希望能够做的是。让用户选择他们感兴趣的过程,然后创建这些点的折线图。

但是,我不完全确定如何解决这个问题。我知道我的所有进程都通过键/值对存储在我的哈希中。所以(“空闲 -> 94%”)等等。我最终想做的是向用户显示一个下拉框......它列出了哈希中的所有键。当用户选择一个特定的键时,它会为他生成一个 GD::Graph。我想,我会在手之前生成每个键的图表并将它们保存为图像。选择一个特定的选项只是将它们链接到正确的图像...

我一直在网上阅读一些有关使用 CGI 实现下拉列表的内容,但还没有走得太远。我正在考虑使用下拉列表创建另一个 HTML 页面,但我不确定如何从 Perl 中存储的哈希中动态填充值。

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 ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-10-08 18:43:33

首先,查找 Data::DumperData::Dump。当您使用数据结构时,这些都会为您提供帮助。

要绘制折线图,​​您需要时间序列数据。听起来您有多个文件,每个文件都是给定时间点的快照。现在您只需对它们进行求和,而不是需要加载所有数据。所以你可以像这样存储它:

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}
    #...
}

然后你的数据结构中就会有时间和程序,这样你就可以随着时间的推移绘制图表。当然,这会使用更多内存(但我猜这并不是真正的问题,除非您有很多这样的文件)。

编辑 1

将所有数据加载到内存后,您可以根据需要对其进行切片。因此,您可以绘制单个程序随时间变化的图表。 CGI.pm 绝对是“老派”(这就是我们在 90 年代中期编写 Perl CGI 的方式),但它应该可以很好地制作您的选择框。您可以使用其 param 方法查看选择的内容:

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

您可以预先生成所有图表,这是否是一个好主意实际上取决于它有多少张图表、每个图表获得多少流量、什么响应时间要求等

(您可能还需要研究诸如 HTML::Template、Template Toolkit、Mason、CGI::Application 和 Catalyst 等内容。另外,您可能想将结果存储在某个地方,而不是重复处理文件,但一次性学习所有这些可能会让人不知所措。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文