bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] Q. about array traversal with user function


From: Ralph Finch
Subject: [bug-gawk] Q. about array traversal with user function
Date: Fri, 21 Feb 2014 10:07:20 -0800

GNU Awk 4.1.0, API: 1.0 (GNU MPFR 3.1.2, GNU MP 5.1.3) under cygwin64.

Not a bug, but a question.

I'm attempting to sort an array with a user-defined function. I have
to define my own function because I want names such as "ANH" and
"RSAN007" to be considered as identical; they should end up next to
each other in the sorted order (these are observed monitoring station
names for water parameters).

To accomplish this I fill an array in the BEGIN block with what names
are equivalent:

    SameStas["ANH"] = " ANC RSAN007 B95020 "
    SameStas["BAC"] = " OBI ROLD21 ROLD24 "
etc.

In the user function (PROCINFO["sorted_in"] = "compPaths") I call
another user function that, given a station name, searches the array
SameStas for equivalent names. Because I have to traverse this second
array (SameStas) while traversing the first array, I save the PROCINFO
sort info as described in the gawk info:

   * The value of `PROCINFO["sorted_in"]' is global. That is, it affects
     all array traversal `for' loops.  If you need to change it within
     your own code, you should see if it's defined and save and restore
     the value:

    save_sorted = PROCINFO["sorted_in"]
    PROCINFO["sorted_in"] = "@ind_str_asc"

and restore PROCINFO before exiting the function:

    PROCINFO["sorted_in"] = save_sorted
    return prefSta

In spite of restoring PROCINFO, I cannot get, for instance, ANH to
appear in the final sorted order next to RSAN007. So finally my
question:

Is saving the value of PROCINFO, and restoring it, valid when PROCINFO
is actively used in traversing an array? The gawk info is unclear on
this.

Thanks,

Ralph Finch
Senior Engineer, WR
California Dept. of Water Resources
Sacramento, CA 95814



reply via email to

[Prev in Thread] Current Thread [Next in Thread]