CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [jeffersno1] perl script to gather dbtable size and memory: Edit Log



FishMonger
Veteran / Moderator

Jul 13, 2013, 6:59 AM


Views: 1521
Re: [jeffersno1] perl script to gather dbtable size and memory

" is not a regex meta character, so there's no need to escape it.

You could use a regex to parse the line as Bill has shown, or you could use the Text::CSV module, which IMO would be a more robust solution.

I might even extend the data structure to be a HoHoH to be able to include the first field as the base/first hash key. I do that because your code appears to indicate that you want to look for a specific first field.

I'll be using the DATA filehandle as Bill did in his example, but take note of the syntax I used on the commented out open call.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::CSV;
use Data::Dumper;

my $db_file = "/var/www/html/graphs/db/rrd/18_dbtables/db_stats_new";
#open my $db_fh, '<', $db_file or die "failed to open '$db_file' $!";

my $csv = Text::CSV->new ( {sep_char => ' '} )
or die "Cannot use CSV: ".Text::CSV->error_diag ();

my %db_hash;
while ( my $row = $csv->getline(*DATA) ) {
#$db_hash{$row->[1]}{$row->[2]} = $row->[3];
$db_hash{$row->[0]}{$row->[1]}{$row->[2]} = $row->[3];
}
print Dumper \%db_hash;


__DATA__
"db_data" "active_sess_fost_frag9" "size" 48610
"db_data" "active_sess_fost_frag9" "memory" 3808418
"db_data" "db_cache_fost_frag9" "size" 107014
"db_data" "db_cache_fost_frag9" "memory" 6958211
"db_data" "cust_sess_frag8" "size" 33733
"db_data" "cust_sess_frag8" "memory" 7186600


Outputs:

Code
$VAR1 = { 
'db_data' => {
'db_cache_fost_frag9' => {
'memory' => '6958211',
'size' => '107014'
},
'active_sess_fost_frag9' => {
'memory' => '3808418',
'size' => '48610'
},
'cust_sess_frag8' => {
'memory' => '7186600',
'size' => '33733'
}
}
};



(This post was edited by FishMonger on Jul 13, 2013, 7:03 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Jul 13, 2013, 7:03 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives