I'm really new to Perl, but I want to basically read in a file to get some data out of it. I want to parse this data into an array. Why an array? Because, I want to use the data to generate an graph (bar or pie of the data).
Here's my code for Perl so far:
#!/usr/bin/perl -w
use warnings;
#Creating an array
my @cpu_util;
#Creating a test file to read data from
my $file = "test.txt";
#Opening the while
open(DATA, $file) || die "Can't open $file: $!\n";
#Looping through the end of the file
while (<DATA>)
{
if (/(\w+)\s+\d+\s+(\d+)\.(\d+)\%/){ #Getting only the "processes"
chomp;
push @cpu_util, split /\t/; #I was hoping this would split the data at the tabs
}
}
close ($file);
foreach $value (@cpu_util) {
print $value . "\n";
}
Here's the file that I am reading in (test.txt):
=========================================
CPU Utilization
=========================================
Name CPU Time CPU Usage
-----------------------------------------
System 7962 3.00%
Adobe Photoshop 6783 0.19%
MSN Messenger 4490 0.01%
Google Chrome 8783 0.02%
Idle 120 94.00%
=========================================
However, what I notice is that I successfully populate the array, but it doesn't split the tabs and give me a multidimensional array. I don't really care about the CPU time field, but I do want the CPU Usage, So I want to print an XY chart with Y-axis having the CPU-usage and x-axis, the process name.
I was hoping to have an array "cpu_util" and have cpu_util[0][0] = System and cpu_util[0][1] = 3.00. Is this even possible? I thought the split /\/t\ would take care of it, but apparently I was mistaken ...