Help: Variables problem

Discussion in 'Perl Misc' started by Amy Lee, Aug 29, 2008.

  1. Amy Lee

    Amy Lee Guest

    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
     
    Amy Lee, Aug 29, 2008
    #1
    1. Advertising

  2. Amy Lee <> writes:

    > 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
     
    Peter Makholm, Aug 29, 2008
    #2
    1. Advertising

  3. Amy Lee

    Amy Lee Guest

    On Fri, 29 Aug 2008 16:52:45 +0200, Peter Makholm wrote:

    > Amy Lee <> writes:
    >
    >> 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

    Huge thanks to you, I have solved this one.

    Amy
     
    Amy Lee, Aug 29, 2008
    #3
  4. Amy Lee

    Ted Zlatanov Guest

    On Fri, 29 Aug 2008 22:44:18 +0800 Amy Lee <> wrote:

    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
     
    Ted Zlatanov, Aug 29, 2008
    #4
  5. Amy Lee

    Amy Lee Guest

    On Fri, 29 Aug 2008 10:21:52 -0500, Ted Zlatanov wrote:

    > On Fri, 29 Aug 2008 22:44:18 +0800 Amy Lee <> wrote:
    >
    > 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
     
    Amy Lee, Aug 29, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,398
    avnrao
    May 7, 2004
  2. Replies:
    1
    Views:
    967
  3. sagejoshua
    Replies:
    16
    Views:
    676
    Michael Winter
    Nov 9, 2005
  4. Replies:
    9
    Views:
    1,015
  5. Replies:
    4
    Views:
    530
    Bruno Desthuilliers
    Apr 30, 2008
Loading...

Share This Page