[comp.lang.perl] Behavior of a2p

nall@sun8.scri.fsu.edu (John Nall) (07/03/90)

In converting a few awk scripts using a2p, it seems that the
behavior of a2p is not exactly what I think it should be.
(This is pretty nit-picky, I realize...:-)  )

As an example:  Consider the following awk script:

	{print NF}

a2p would convert this into the following (leaving out some of
the header material):

	$[ = 1;
	$, = ' ';
	$\ = "n";

	while (<>) {
		$Fld = split(' ', $_, 999);
		print $#Fld;
	}

Now consider one line of data, consisting of the following:

	1 2 3 4 5

The awk version would print 5, saying there are 5 fields.

The perl version, produced by a2p, would print 6, saying
there are 6 fields.  One can "fix" the perl version in either
one of two ways: (1) by adding chop; after the while(<>) {
or (2) by changing the split to "split(' ',$_).  The behavior
of split without the LIMIT being specified is, of course,
documented.

So it would seem that the scripts produced by a2p, in order
to give the same result as the original awk script, should
do one of these two things, since it is fairly common for awk
scripts to assume that NF is going to be the actual number
of fields.


--
John W. Nall		| Supercomputation Computations Research Institute
nall@sun8.scri.fsu.edu  | Florida State University, Tallahassee, FL 32306
"They said it couldn't be done/they said nobody could do it/
But he tried the thing that couldn't be done!/He tried - and he couldn't do it"

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/04/90)

In article <182@sun13.scri.fsu.edu> nall@sun8.scri.fsu.edu (John Nall) writes:
: In converting a few awk scripts using a2p, it seems that the
: behavior of a2p is not exactly what I think it should be.
: (This is pretty nit-picky, I realize...:-)  )
: 
: As an example:  Consider the following awk script:
: 
: 	{print NF}
: 
: a2p would convert this into the following (leaving out some of
: the header material):
: 
: 	$[ = 1;
: 	$, = ' ';
: 	$\ = "n";
: 
: 	while (<>) {
: 		$Fld = split(' ', $_, 999);
: 		print $#Fld;
: 	}
: 
: Now consider one line of data, consisting of the following:
: 
: 	1 2 3 4 5
: 
: The awk version would print 5, saying there are 5 fields.
: 
: The perl version, produced by a2p, would print 6, saying
: there are 6 fields.  One can "fix" the perl version in either
: one of two ways: (1) by adding chop; after the while(<>) {
: or (2) by changing the split to "split(' ',$_).  The behavior
: of split without the LIMIT being specified is, of course,
: documented.
: 
: So it would seem that the scripts produced by a2p, in order
: to give the same result as the original awk script, should
: do one of these two things, since it is fairly common for awk
: scripts to assume that NF is going to be the actual number
: of fields.

After the next patch, a2p will throw in a chop if it sees NF.
Way 2 would run into the problem that NF could be too small
if trailing fields were null.

Larry