Julie Xu wrote:

Greeting,

I am trying to create a simple perl script to read a text file (8 columns
separated by space) and output the colume6,8.


The file format is:
Entry11 entry12 entry13 entry14 entry15 entry16 entry17 entry18
Entry21 entry22 entry23 entry24 entry25 entry26 entry27 entry28
..................

The script is following:
#!/usr/bin/perl -w
use perltext;
use strict;
use Fcntl ':flock'; # contains LOCK_EX (2) and LOCK_UN (8) constants

my $buffer;
my $file;

$file = $ARGV[0];


You might want to add a error check here if no command line arguments are passed



open(INFILE, $file);



Make sure to check if open succeeded, like this open (INFILE, $file) or die "failed to open $file for reading, $!\n"; For information on $! perldoc -f open perldoc perlvar

# request an exclusive lock on the file.
flock(INFILE, LOCK_EX);


Since you are just reading from the file, a shared lock should suffice flock (INFILE, LOCK_SH) or die "failed to lock file..."; Make sure to check for error here to

# read in each line from the file
while (<INFILE>)
{
 #split out the fields in the line
 @entry = split / /;


You can just write this as @entry = split; #perldoc -f split, check out the default behaviour of split Also, shouldn't there be a my before @entry

print "Entry: $entry[6] Entry: $entry[8]\n";


Your problem is here, you want elements 6 (array index 5) and 8 (array index 7). Array indexes start from 0 by default.
print "Entry: $entry[5] Entry: $entry[7]\n";



}
print "Entry6: $entry[6] Entry8: $entry[8]\n";



Same here


# unlock the file.
flock(INFILE, LOCK_UN);
close(INFILE);


the output I want is: Entry: xxxxxx Entry: xxxxxx

Could anyone help me with this script since it can not get correct output?

Any comments will be appreciated

Thanks in advance

Julie





-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to