Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Format Syntax error on "."

by jcain (Initiate)
on Feb 21, 2012 at 00:31 UTC ( #955195=perlquestion: print w/ replies, xml ) Need Help??
jcain has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to do some basic formatting for specific output that is being parsed from top. However, I have a syntax error on line 52, near "." I am using perl 5.10.1

I'm sure I am missing something obvious, but I am relatively new with perl and the manual pages don't appear to offer an obvious explanation. I have tried a few other places for the format code, but I continually get the "." syntax error.

#!/usr/bin/perl use strict; use warnings; use English; #use FileHandle; ## Declarations ## my @topps = qx/top -b -n 1/; my @iostatc = qx/iostat -t -m/; my @iostatd= qx/iostat -m -x | grep -v Linux/; my @top; my @topout; ## End Delcarations ## ## Grab top ps info on any processes using cpu ## foreach (@topps) { chomp; if ($_ =~ /^(\s)*\d+.*/) { push @top, $_; } elsif ($_ =~ /^.*:.*/) { # Print out tops header info print "$_\n"; } } # Some simple headers #my @tophat = ( "User", "Command", "CPU%", "Mem%" ); #$topout[0] = [ @tophat ]; foreach (@top) { my $i = 0; my @line = split; if ($line[8] != 0) { my @tmp = ( "$line[1]", "$line[11]", "$line[8]%", "$line[9]%" ); $topout[$i] = [ @tmp ] ; } $i++; } ## End top grab ## ## Format and Print the top info ## foreach my $t (@topout) { my $index = 0; foreach my $ps (@$t) { my $user; my $cmd; my $cpu; my $mem; format STDOUT_TOP = @<<<<<<<<<<<<<<< @|||||||||| @||||| @||||| User Command CPU% Mem% . $^ = 'STDOUT_TOP'; format STDOUT = @<<<<<<<<<<<<<<< @|||||||||| @##### @##### $user, $cmd, $cpu, $mem . $~ = 'STDOUT'; if ($index == 0) { $user = "$ps"; } elsif ($index == 1) { $cmd = "$ps"; } elsif ($index == 2) { $cpu = "$ps"; } elsif ($index == 3) { $mem = "$ps"; } $~ = 'topinfo'; write; $index++; } } ## iostat cpu and device info ## sub bang { for my $i (1..80) { print "#"; } print "\n"; } bang(); foreach (@iostatc) { chomp; print "$_\n"; } bang(); foreach (@iostatd) { chomp; unless ($_ =~ /^[(avg.*)|(\s+\d+.*)]/) { unless ($_ =~ /^\d+/) { print "$_\n"; } } } bang();

Any suggestions are improvements are welcome in addition to solving the formating error.

Thanks.

Comment on Format Syntax error on "."
Download Code
Re: Format Syntax error on "."
by kielstirling (Scribe) on Feb 21, 2012 at 00:54 UTC
    Hi,

    Problem is the % in the labels.

Re: Format Syntax error on "."
by kcott (Abbot) on Feb 21, 2012 at 01:03 UTC

    I quoted the entire line 51 and the problem went away. Just so that's clear, checking syntax of:

    ... format STDOUT_TOP = @<<<<<<<<<<<<<<< @|||||||||| @||||| @||||| q{User Command CPU% Mem%} . $^ = 'STDOUT_TOP'; ...

    gives

    $ perl -c pm_format_syntax.pl pm_format_syntax.pl syntax OK

    I haven't used format in almost 15 years, so I can't give a definitive reason as to why this fixed the problem. My rationale for trying the quoting was the presence of the % characters in the line immediately prior to the line which generated the error. Other monks may provide a more detailed explanation.

    -- Ken

      Excellent. I was staring it this for 2 moments too long. It was in fact the %.
Re: Format Syntax error on "."
by Anonymous Monk on Feb 21, 2012 at 02:08 UTC
      just put the fieldline of format in quote will fix the issue.
      format STDOUT_TOP = @<<<<<<<<<<<<<<< @|||||||||| @||||| @||||| "User Command CPU Mem%" . $^ = 'STDOUT_TOP'; format STDOUT = @<<<<<<<<<<<<<<< @|||||||||| @##### @##### $user, $cmd, $cpu, $mem .
Re: Format Syntax error on "."
by DrHyde (Prior) on Feb 21, 2012 at 10:36 UTC
    Formats! Gosh, how quaint! sprintf() is the preferred - and far more readable - alternative.

    Also, which is line 52? I doubt that many people can be bothered to count lines for you.

      For a usage like we see in the OP, sure, sprintf() is the way to go.

      Have you even written a multi-page report using nothing but sprintf()? It's painful but formats make it bearable.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://955195]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-10-31 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls