On 2/18/2008 9:38 AM, Janis wrote:
> On 18 Feb., 15:14, Ed Morton <mor...@[EMAIL PROTECTED]
> wrote:
>
>>On 2/18/2008 6:57 AM, Spiros Bousbouras wrote:
>>
>>
>>>On page 194 of "Effective AWK programming" we see the
>>>following programme:
>>
>>># rewind.awk --- rewind the current file and start over
>>>function rewind( i)
>>>{
>>> # ****ft remaining arguments up
>>> for (i = ARGC; i > ARGIND; i--)
>>> ARGV[i] = ARGV[i-1]
>>> # make sure gawk knows to keep going
>>> ARGC++
>>> # make current file next to get done
>>> ARGV[ARGIND+1] = FILENAME
>>> # do it
>>> nextfile
>>>}
>>
>>>Isn't the statement ARGV[ARGIND+1] = FILENAME
>>>superfluous ? In the loop when i gets the
>>>value ARGIND + 1 then the assignment
>>>ARGV[ARGIND + 1] = ARGV[ARGIND] will be executed
>>>and ARGV[ARGIND] always has the same value as
>>>FILENAME
>>
>>But what if the current file is the last one? Then i = ARGC = ARGIND at
the
>>start of the loop, so the loop's never entered so THEN that final
assignment is
>>required. Although that works in this context since the functiona name
implies
>>it MUST be called while parsing a file, use of FILENAME meanse you can't
execute
>>this type of general "****ft the arguments" function in a BEGIN section
so I'm
>>not a big fan.
>
>
> While filename is just unset in the BEGIN block it seems there's a
> more fatal problem with 'nextfile' in rewind()...
>
> $ xgawk 'BEGIN{nextfile}'
> xgawk: error: `nextfile' used in BEGIN action
>
Right. I was thinking more of the general "****ft the arguments" case where
this:
function ****ft( i)
{
# ****ft remaining arguments up
for (i = ARGC; i > ARGIND; i--)
ARGV[i] = ARGV[i-1]
# make sure gawk knows to keep going
ARGC++
# make current file next to get done
ARGV[ARGIND+1] = FILENAME
}
wouldn't work, but this would:
function ****ft( i)
{
# ****ft remaining arguments up
for (i = ++ARGC; i > ARGIND; i--)
ARGV[i] = ARGV[i-1]
}
Regards,
Ed.


|