Help: Variables problem

A

Amy Lee

Hello,

I'm writing a script to parse the contents of /proc/loadavg file. And the
file is like this:

0.22 0.11 0.03 1/92 2653

And I hope my script output is like this:

1 minute loadavg: 0.22
5 minutes loadavg: 0.11
10 minutes loadavg: 0.03
Running processes: 1 processes
Total processes: 92 processes

And there's my codes:

open LOADAVG, "<", "/proc/loadavg"
or die RED "Can't open /proc/loadavg. $!";
while (<LOADAVG>)
{
<LOADAVG> =~ /(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\/(\d+)\s+\d+/;
print "1 minute loadavg: $1.$2\n";
print "5 minutes loadavg: $3.$4\n";
print "10 minutes loadavg: $5.$6\n";
print "Running processes: $7 processes\n";
print "Total processes: $8 processes\n";
}
close LOADAVG;

But when I excute this codes such errors shows:

Use of uninitialized value in concatenation (.) or string at ./soms.pl line 16, <LOADAVG> line 1.
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 16, <LOADAVG> line 1.
1 minute loadavg: .
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 17, <LOADAVG> line 1.
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 17, <LOADAVG> line 1.
5 minutes loadavg: .
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 18, <LOADAVG> line 1.
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 18, <LOADAVG> line 1.
10 minutes loadavg: .
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 19, <LOADAVG> line 1.
Running processes: processes
Use of uninitialized value in concatenation (.) or string at ./soms.pl line 20, <LOADAVG> line 1.
Total processes: processes

Could you tell me what happens? And how to fix it.

Huge thanks!

Best Regards,

Amy
 
P

Peter Makholm

Amy Lee said:
And there's my codes:

open LOADAVG, "<", "/proc/loadavg"
or die RED "Can't open /proc/loadavg. $!";

On my system this pseudo file consists of one and only one line
while (<LOADAVG>)

Which you read into $_ here.
{
<LOADAVG> =~ /(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\/(\d+)\s+\d+/;

As you allready has read the only line in the file this read opration
returns undef. So what you actually do is to try to match on the
undefined string. This fails of course and thus leaves $1 to $9
undefined.
Could you tell me what happens? And how to fix it.

Try matching the line you just read instead of matching on the next
line.

//Makholm
 
A

Amy Lee

On my system this pseudo file consists of one and only one line


Which you read into $_ here.


As you allready has read the only line in the file this read opration
returns undef. So what you actually do is to try to match on the
undefined string. This fails of course and thus leaves $1 to $9
undefined.


Try matching the line you just read instead of matching on the next
line.

//Makholm
Huge thanks to you, I have solved this one.

Amy
 
T

Ted Zlatanov

AL> I'm writing a script to parse the contents of /proc/loadavg file. And the
AL> file is like this:

AL> 0.22 0.11 0.03 1/92 2653

AL> And I hope my script output is like this:

AL> 1 minute loadavg: 0.22
AL> 5 minutes loadavg: 0.11
AL> 10 minutes loadavg: 0.03
AL> Running processes: 1 processes
AL> Total processes: 92 processes

AL> And there's my codes:

AL> open LOADAVG, "<", "/proc/loadavg"
AL> or die RED "Can't open /proc/loadavg. $!";
AL> while (<LOADAVG>)
AL> {
AL> <LOADAVG> =~ /(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\/(\d+)\s+\d+/;
AL> print "1 minute loadavg: $1.$2\n";
AL> print "5 minutes loadavg: $3.$4\n";
AL> print "10 minutes loadavg: $5.$6\n";
AL> print "Running processes: $7 processes\n";
AL> print "Total processes: $8 processes\n";
AL> }
AL> close LOADAVG;

Use the split ' ' syntax to get individual fields (you'll need a further
split for field 4, of course). You can also consider Regexp::Common to
extract numbers.

Or install the CPAN Linux::loadavg module.

Ted
 
A

Amy Lee

AL> I'm writing a script to parse the contents of /proc/loadavg file. And the
AL> file is like this:

AL> 0.22 0.11 0.03 1/92 2653

AL> And I hope my script output is like this:

AL> 1 minute loadavg: 0.22
AL> 5 minutes loadavg: 0.11
AL> 10 minutes loadavg: 0.03
AL> Running processes: 1 processes
AL> Total processes: 92 processes

AL> And there's my codes:

AL> open LOADAVG, "<", "/proc/loadavg"
AL> or die RED "Can't open /proc/loadavg. $!";
AL> while (<LOADAVG>)
AL> {
AL> <LOADAVG> =~ /(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\/(\d+)\s+\d+/;
AL> print "1 minute loadavg: $1.$2\n";
AL> print "5 minutes loadavg: $3.$4\n";
AL> print "10 minutes loadavg: $5.$6\n";
AL> print "Running processes: $7 processes\n";
AL> print "Total processes: $8 processes\n";
AL> }
AL> close LOADAVG;

Use the split ' ' syntax to get individual fields (you'll need a further
split for field 4, of course). You can also consider Regexp::Common to
extract numbers.

Or install the CPAN Linux::loadavg module.

Ted
Thanks, it seems that your method id pretty good.

Regards,
Amy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top