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: Beginner:
Parsing a line question

 



aixdude
New User

Apr 19, 2007, 9:46 AM

Post #1 of 2 (481 views)
Parsing a line question Can't Post

I thought I had a good grip on how to parse line by line reading from an input file, and this script works so far except for the field delimiter portion of it.

I thought using this method of reading a file I should be able to pull out fields from a text line by addressing them like awk - $1,$2, etc... but I get nothing at all.



I've tried passing different files to this program and get the same results. When I print my $stdrec var I DO see the whole line, but when I try and get a field from it, no go... what am I missing?



Please note, the if is always going to be true based on my input, it is just there as a test, the probflag prints a '1' which is what I would expect, but I get no return from the $percent var, where I would expect a number since the 5th field in an output line from lsps is the % used.

I have tried passing it a simple text file as well: "The quick brown fox jumped over the lazy dog" and I get the same results... do I need to read it as an array?

I don't want an alternative way in awk or shell to do this, I would like to learn why perl is not working for me...



Thanks,



#!/usr/bin/perl



#set global vars
$LogDir="/var/adm";
$Date=`date`;

#get paging usage
system ("rm $LogDir/paging.tmp");
system ("lsps -a > $LogDir/paging.tmp");
$Pagefile="$LogDir/paging.tmp";

#verify and create new file var
open(PAGEOUT,$Pagefile) || die "can't open paging.out";

# begin loop to read paging
while (<PAGEOUT>)
{
next if /Space/; #skip the header from lsps
$stdrec=$_;
chop $stdrec;
$percent="";
$probflag=0;
if ( $stdrec=~/M/ )
{
$probflag=1;
$percent=$5;
};

print "$probflag $percent \n"; #DEBUG
}; #end while


Output is a 1 only, no other prints...


osmodius
Novice

Apr 19, 2007, 10:50 AM

Post #2 of 2 (475 views)
Re: [aixdude] Parsing a line question [In reply to] Can't Post


Code
        if ( $stdrec=~/M/ ) 
{
$probflag=1;
$percent=$5;
};

print "$probflag $percent \n"; #DEBUG
}; #end while


You're asking if $stdrec has the letter M in it, and then you're trying to assign $percent the value of $5. $5 does not exist. As a windows junkie I'm not too sure what awk does, but if it splits at spaces and treats those as fields.. you can mimic this by using the split function on your string and manipulating the array, like so..


Code
        if ( $stdrec=~/M/ ) 
{
@stdrec = split(/ /, $stdrec);
$probflag=1;
$percent=$stdrec[4];
};

print "$probflag $percent \n"; #DEBUG
}; #end while



Need some real-time help? Shove admin@ub3r.net on yer MSN.

 
 


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

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