For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > Re: generation of sequences









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: generation of sequences
Sebastian Luque

2005-11-14, 6:56 pm

"William James" <w_a_x_man@yahoo.com> wrote:

[...]

> # Input: 26/04/05 start 08:30:00
> # Output: 2005 04 26 08 30 30
> function fix_date_string( s, a )
> { split( s, a, /[ :\/]/ )
> return sprintf( "%s %s %s %s %s %s",
> 2000+a[3], a[2], a[1], a[5], a[6], a[7] )
> }


> function get_start_time( s )
> {
> match( s, /Date / )
> return mktime( fix_date_string( substr(s, RSTART+RLENGTH) ))
> }


> function format_time( timestamp )
> { return strftime( "%d/%m/%Y, %H:%M:%S", timestamp )
> }


In this case, fix_date_string is getting a single argument, whereas the
function has two arguments in its definition. Reading the manual (and a
little testing) I gather there's no harm in removing the second argument
(a) from the function's definition. Is this correct?


Cheers,

--
Sebastian P. Luque
Ed Morton

2005-11-14, 6:56 pm

Sebastian Luque wrote:
> "William James" <w_a_x_man@yahoo.com> wrote:
>
> [...]
>
>
>
>
>
>
>
>
> In this case, fix_date_string is getting a single argument, whereas the
> function has two arguments in its definition. Reading the manual (and a
> little testing) I gather there's no harm in removing the second argument
> (a) from the function's definition. Is this correct?


No, that's how you force a "local" variable declaration. Look:

$ awk 'function foo(){i=7}BEGIN{for (i=1;i<=6;i++){print i;foo()}}'
1
$ awk 'function foo( i){i=7}BEGIN{for (i=1;i<=6;i++){print i;foo()}}'
1
2
3
4
5
6

Even if it has no effect today, it could protect you in future. It's
common to put a tab before the first of these "dummy" arguments so
people know what it is without thinking too hard!

Ed.
Janis Papanagnou

2005-11-14, 6:56 pm

Ed Morton wrote:
>
> [ "local" variable declaration ]
>
> Even if it has no effect today, it could protect you in future. It's
> common to put a tab before the first of these "dummy" arguments so
> people know what it is without thinking too hard!


I know it's common practice but it's not a good practice, IMO, since
depending on the position of the tab it may not to be distinguishable
from a single blank. The local variables should be visibly separated
from the function arguments, though.

Janis
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com