Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Perl script

Status
Not open for further replies.

Disruptive

Programmer
Mar 8, 2005
40
GB
Hi

I am having trouble trying to read in name, value pair data from a text file. I want each name and value on each line and be able to check the value of each name or value indepently (stored in sep varables). I seem to have tied myself in knots trying to get this working.

#!/usr/bin/perl

$LOGFILE = "myfile.txt";
open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
chomp($line); # remove the newline from $line.
# do line-by-line processing.
# print $line;
($name, $value) = split(' ', $line);
print $name $value;

}
 
Try this (see changes in red):

Code:
#!/usr/bin/perl

$LOGFILE = "myfile.txt";
open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
    chomp($line);              # remove the newline from $line.
    # do line-by-line processing.
    # print $line;
   ($name, $value) = split(' ', $line);
    print [COLOR=red]"[/color]$name $value[COLOR=red]"[/color];
 
}

- George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top