Re: Help with script



Andy wrote:
On Feb 28, 1:15 pm, "John W. Krahn" <some...@xxxxxxxxxxx> wrote:
Andy wrote:

I am pretty new here, tyring to learn as I go.
I have a lil project I am trying to setup
Basically : below is a piece of the file
Has three fields Seperated by ~
6081703039~1~My Own Company
3081709039~1~DeweyCheetham&Howe
9081710039~1~One Bad Firm
2081757039~1~IRSUSUCK
1082445039~1~
Basically I need the file to only show the Field that has all data
I am new so not sure if my syntax is correct
If field 3 is blank then don't show?
Can anyone help me write a Perl script to acocmplish this ?
while ( <FH> ) {
chomp;
my @fields = split /~/, $_, -1;

if ( 3 != @fields ) {
warn "Error: should be 3 fields, record contains " . @fields .
" fields instead.\n";
next;
}

if ( 3 != grep length, @fields ) {
warn "Error: one of the fields is empty.\n";
next;
}
# Or, just the third field:
unless ( length $fields[ 2 ] ) {
warn "Error: the third field is empty.\n";
next;
}

# process the data
}

John

Thank you Very Much

I take it I have to turn around and

Add

!#/usr/bin/perl -w

Better to add:

#!/usr/bin/perl
use warnings;
use strict;

Question How does the script know that I want it to Scrub a particular
file.

It doesn't. The example posted is incomplete.

In this case the Above snippet is from a file called

msus.dat

Open the file first:

open FH, '<', 'msus.dat' or die "Cannot open 'msus.dat' $!";



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages