Operating System - HP-UX
1828474 Members
3095 Online
109978 Solutions
New Discussion

perl script not working the way I expect

 
SOLVED
Go to solution
eric stewart_2
Advisor

perl script not working the way I expect

Here is the input 3 lines:
"MEMNAME"!"NAME"!"VARNUM"!"LABEL"!"ROLE"!"ORIGIN"!"COMMENTS"!"CRFPAGE"!"CODES"
"COMMENT"!"AGE"!"16"!"AGE"!" "!" "!" "!" "
"COMMENT"!"CNTRY"!"20"!"DECODE, CTRY"!" "!" "!" "!" "
Here is the output 3 lines:
_"MEMNAME"_"NAME"_"VARNUM"_"LABEL"_"ROLE"_"ORIGIN"_"COMMENTS"_"CRFPAGE"_"CODES"_
_"COMMENT"_"AGE"_"16"_"AGE"_" "_" "_"integer ((open treatment start date - birth date) / 365.25)."_" "__
_"COMMENT"_"CNTRY"_"20"_"DECODE, CTRY"_" "_" "_" "_" "__

Here is the perl program:
#!/usr/contrib/bin/perl
open(VARLIST, "/tmp/varlist.csv")
|| die " can not open comman comments file\n";
while() { chop;
($MEMNAME,$NAME,$VARNUM,$LABEL,$ROLE,$ORIGIN,$COMMENTS,$CRFPAGE,$CODES) = split(/!/,$_);
printf "_%s_%s_%s_%s_%s_%s_%s_%s_%s_\n",$MEMNAME,$NAME,$VARNUM,$LABEL,$ROLE,$ORIGIN,$COMMENTS,$CRFPAGE,$CODES;
};
close VARLIST;

Why do I get two underscores on the lines after the first. I would expect only 1?
Puzzled. New PERL user. TIA
6 REPLIES 6
Tom Maloy
Respected Contributor
Solution

Re: perl script not working the way I expect

The first line has 9 data entries.
Lines 2 and 3 have only 8 data entries.
So for lines 2 and 3, $CODES is null, hence two underscores together.

Tom
Carpe diem!
Rodney Hills
Honored Contributor

Re: perl script not working the way I expect

After the "while(" statment, add a line-
chop;

Each line has an end-of-line character after it has been read in.

By the way- /usr/contrib/bin/perl is version 4 of perl, you should get version 5 from the software porting and archive.

-- Rod Hills
There be dragons...
eric stewart_2
Advisor

Re: perl script not working the way I expect

Tom,
I feel embarrassed that the answer was simple.
Thanks
Tom Maloy
Respected Contributor

Re: perl script not working the way I expect

Eric,

That's why another pair of eyes is good.

One fellow labored all night to debug something. When I saw him the next morning, I pointed out that he had a capital oh (O) instead of a zero (0). He yelled a lot.

Tom
Carpe diem!
H.Merijn Brand (procura
Honored Contributor

Re: perl script not working the way I expect

Rodney, please unlearn to use chop :)
learn to use chomp instead

chop cuts off the last character. whatever it is.
chomp is safe, it cuts of the value of $/, but only if present

chop;chop; is destructive
chomp;chomp; is not
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: perl script not working the way I expect

Procura,

I usually do, but I only said "chop" because he was using perl 4.

"chomp" didn't appear until perl 5.

-- Rod Hills
There be dragons...