user1125595
user1125595

Reputation: 11

Perl Check Text Line

I have a Perl code that must read a text file that contains distinct lines.

For example:

blah-blah-blah yakkity-yak gobbledy_gook

doohickey thingamabob watchamacallit

one two three

Some days this file may have several lines, some days only one line, but each line will contain a fixed number of words with each separated by an empty space.

Basically what I have now is:

my @info = split (" ", $line);

Then a scalar check on @info and if it is NOT exactly three -- exit.

Is there a quicker and may be more elegant way? Thnx.

Roderick

Upvotes: 0

Views: 104

Answers (2)

choroba
choroba

Reputation: 241918

The conversion to number is done automagically if you compare an array to a number:

die "Not enough (or too much) information\n" unless @info == 3;

Upvotes: 1

SAN
SAN

Reputation: 2247

This one liner prints all the lines with word count not equal to 3

perl -lane 'print if $#F!=2' FILE_NAME

Upvotes: 0

Related Questions