Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: undefined hash elements

by Random_Walk (Prior)
on Dec 18, 2013 at 20:58 UTC ( [id://1067708]=note: print w/replies, xml ) Need Help??


in reply to undefined hash elements

On my current OS, Linux Mint, the following code works. Note I take the headers from the first line of df and use them. I then have a small abstraction of a name hash to map mount and percent to appropriate fields. I think your list of headers came from another OS than the one you are running on, and that is leading down the wrong path.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # a couple of headers that vary from df to df my %name = ( percent => 'Use%', mount => 'Mounted', ); # my @headers = qw(name size used free capacity mount); my @df = `df -k`; my @headers = split /\s+/, shift @df; my %devices; for my $line (@df) { print $line; my %info; @info{@headers} = split /\s+/, $line; # note the hash slice $info{capacity} = _percentage_to_decimal($info{$name{percent}}); $devices{ $info{$name{mount}} } = \%info; } # Change 12.3% to .123 sub _percentage_to_decimal { my $percentage = shift; $percentage =~ s{%}{}; return $percentage / 100; } print Dumper \%devices;

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: undefined hash elements
by MrTEE (Novice) on Dec 18, 2013 at 21:07 UTC

    Yeah It works for me at home as well. the DF has long values at work, they get kicked to another line. That is just what DF does. . The df -k | grep -v var cleans up - but i have gotten these undefined errors before. it is a linux box.

    [capsper@casperbox .wjohnson]$ uname -a Linux casper25s 2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:52:25 EST 2011 + x86_64 x86_64 x86_64 GNU/Linux

    This is the data dumper - is there a way to get rid of the undef's ?

    [icapsper@casperbox .wjohnson]$ ./get_df.just_capacity $VAR1 = {}; Use of uninitialized value in substitution (s///) at ./get_df.just_cap +acity line 25. Use of uninitialized value in division (/) at ./get_df.just_capacity l +ine 26. Use of uninitialized value in hash element at ./get_df.just_capacity l +ine 18. $VAR1 = { '' => { 'free' => undef, 'mount' => undef, 'used' => undef, 'name' => '/dev/mapper/VolGroup00-LogVol00', 'capacity' => '0', 'size' => undef } };

      I dump out the %info and %devices hashes

      use Data::Dumper ; my %devices; for my $line (@df) { use Data::Dumper ; my %info; print Dumper (\%info); @info{@headers} = split /\s+/, $line; # note the hash slice $info{capacity} = _percentage_to_decimal($info{capacity}); $devices{ $info{mount} } = \%info; print Dumper(\%devices); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1067708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found