bottom...
Felix Geerinckx wrote:
> on Tue, 20 Aug 2002 16:22:31 GMT, Jessica wrote:
>
>> I am attempting to extract a two words or fields from every line of a
>> tab delimited file. Sometimes the words that I need are in fields 1 &
>> 2 and other time they are in 2 & 3. The lines look something like
>> this:
>>
>> TypeOfApp ApplicationName RunStatus
>> ---------------------------------------------------------------------
>>
>> application run_me_now complete
>> application2 ran_me_yesterday complete/errors
>> need_to_run_tomorrow failed
>>
>> I am in need of just extracting the ApplicationName and RunStatus
>> from the lines and placing them in variables so that I can do things
>> with them.
>>
>> I think I'm smart enought to know how to get the file opened and get
>> the lines into a while loop like so:
>>
>> open (FILE, ./datafile);
>
> Unfortunately, this is not the case:
>
> You forgot to turn on warnings and strictures.
> You didn't quote your filename.
> You didn't check the return value of the open-call.
>
>
> Try this:
>
> #! perl -w
> use strict;
>
> open FILE, './datafile' or die "Could not open file: $!";
>
> while (<FILE>) {
> chomp;
>
> # skip blank lines and column headers
> next if /^$/;
> next if /^---------/;
> next if /^TypeOfApp/;
>
> # keep last two fields
> my ($when, $result) = (split ' ')[-2,-1];
> print "$when: $result\n";
> }
>
>
i would just:
#!/usr/bin/perl -w
use strict;
open(FILE,'./datafile') || die("can't open ./datafile: $!\n");
#-- discard header
#-- to remove the if checks inside the while loop to improve performance
<FILE>;
<FILE>;
<FILE>;
while(<FILE>){
chop;
my($schedule,$result) = (split(/\s+/))[-2,-1];
print "$schedule $result\n";
}
close(FILE);
david
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]