function foo(s) {split(s,A);return A}
BEGIN { x = foo("the quick brown fox");for (i in x) print i,x[i] }
Error message in GAWK is "array used in scalar context" on the "return A"
line. I'm just curious - how intentional is this limitation?
Is there any particular reason for it to not work?
Tested in GAWK 3.1.4 - maybe fixed in later versions?
AFAIK it's not a bug (it's a gawk feature), so I treat array name as global
'cause the name is 'tainted' once used for array, no longer usable as scalar.
Grant.
You could argue that returning an array is bad design since it overloads the
return from the function with the array contents plus an indication of
success/fail (which itself might be overloaded as a count of array entries). In
this specific case, a better design for the function might be:
awk 'function foo(s,A, c) {c=split(s,A); return c}
BEGIN { n = foo("the quick brown fox",x);for (i=1;i<=n;i++) print i,x[i] }'
1 the
2 quick
3 brown
4 fox
I think I could get behind the argument that any function you want to return an
array would actually be better designed as a function that populates the array
as a parameter and returns the number of elements in that array.
Ed.
Hey, I do that, even in TAWK! Not always, but this specific case is one
example. I have a custom split() that breaks up strings on whitespace, but
NOT if the whitespace is within a literal string or escaped. A passed-in
name is populated as an array, and the function itself returns the number of
items found.
However there are also plenty of times I return an array directly. You're
right in that this makes error conditions harder to detect. Sometimes I set
a global flag. Other times I return different types and rely on
run-time-type-identification (RTTI) to indicate what happened.
Although I've had minor problems with that in TAWK 5, which occasionally
seems to mess with types during function calls in ways that TAWK 4 never
does. Integers somehow becoming floats, IIRC.
- Anton Treuenfels
Historical awk didn't have arrays as return values and so neither
does gawk. I don't see a major reason to add it; it's better
to pass in an array to be populated.
I also think that the gawk code is fragile enough that trying
to do this isn't a good idea in any case.
Thanks,
Arnold
P.S. You should move to 3.1.7 or CVS gawk-stable. 3.1.4 is pretty old.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
Good answer. That answers my basic question.
>It's better to pass in an array to be populated.
This comment belongs in the "alternatives" thread, not here.
>I also think that the gawk code is fragile enough that trying
>to do this isn't a good idea in any case.
Good point.
>Thanks,
Thank you (Miss Howe!)