GAWK: Question on using filefuncs.so (creating arrays in extension functions) 
Author Message
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

In GAWK 3.10, under Linux, I compiled the filefuncs extension library, and
it works fine.  However, I have one little "curiosity type" question.

testff.awk contains the following lines:

    data[1] = 1
    print "Info for testff.awk"
    ret = stat("testff.awk", data)

I'm curious about the need for the first line - where the "data" array is
setup, with dummy data.  If you leave that line out, then the call to the
"stat" function (which is defined by the shared lib) fails with a "fatal
error: internal error/Abort".  It seems that you can't create the array
from scratch within the extension function; it has to have at least one
element in it prior to calling the function in the lib.

I am curious as to why this is.  Why it doesn't work w/o that line.



Tue, 13 Apr 2004 07:52:36 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

Quote:

> In GAWK 3.10, under Linux, I compiled the filefuncs extension library, and
> it works fine.  However, I have one little "curiosity type" question.

> testff.awk contains the following lines:

>     data[1] = 1
>     print "Info for testff.awk"
>     ret = stat("testff.awk", data)

> I'm curious about the need for the first line - where the "data" array is
> setup, with dummy data.  If you leave that line out, then the call to the
> "stat" function (which is defined by the shared lib) fails with a "fatal
> error: internal error/Abort".  It seems that you can't create the array
> from scratch within the extension function; it has to have at least one
> element in it prior to calling the function in the lib.

> I am curious as to why this is.  Why it doesn't work w/o that line.

I guess that this is so that data is set to have array type, presumably
otherwise it would be treated as a scalar.  This would be in keeping
with awk's "once an array name, always an array name ..."

Peter
--
--
Sent using an unregistered copy of RMRNews v1.02
Check out our website at http://www.rmrsoft.com/
for other high quality software for EPOC machines.



Wed, 14 Apr 2004 02:26:30 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)


...

Quote:
>> I'm curious about the need for the first line - where the "data" array is
>> setup, with dummy data.  If you leave that line out, then the call to the
>> "stat" function (which is defined by the shared lib) fails with a "fatal
>> error: internal error/Abort".  It seems that you can't create the array
>> from scratch within the extension function; it has to have at least one
>> element in it prior to calling the function in the lib.

>> I am curious as to why this is.  Why it doesn't work w/o that line.

>I guess that this is so that data is set to have array type, presumably
>otherwise it would be treated as a scalar.  This would be in keeping
>with awk's "once an array name, always an array name ..."

Yes, but it doesn't quite make sense - because the thing is used as an
array name later on.  I'm pretty sure (have lots of evidence for this, but
can't quite specify it right now, off hand) that it (GAWK) scans the entire
source and figures out which things are arrays before executing anything.
So, it should know that data is an array, even though nothing in it has
been initialized yet.

BTW, I found this amusing (on a Solaris system):

% nawk 'BEGIN{x="abc";split("abc",x)}'
nawk: can't assign to x; it's an array name.
 source line number 1
% gawk 'BEGIN{x="abc";split("abc",x)}'
gawk: cmd. line:1: fatal: attempt to use scalar `x' as array
%

TAWK has no such problems.  Observe:

% tawk 'BEGIN{x="abc";print typeof(x);split("abc",x);print typeof(x)}'
string
array
%



Wed, 14 Apr 2004 02:39:16 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

Quote:



<snip>
> >> I am curious as to why this is.  Why it doesn't work w/o that line.

> >I guess that this is so that data is set to have array type, presumably
> >otherwise it would be treated as a scalar.  This would be in keeping
> >with awk's "once an array name, always an array name ..."

> Yes, but it doesn't quite make sense - because the thing is used as an
> array name later on.  I'm pretty sure (have lots of evidence for this, but

Surely your gawk example below confirms my comment.  "x" has been given
scalar type, the subsequent split() then barfs as a result.

Quote:
> can't quite specify it right now, off hand) that it (GAWK) scans the entire
> source and figures out which things are arrays before executing anything.
> So, it should know that data is an array, even though nothing in it has
> been initialized yet.

> BTW, I found this amusing (on a Solaris system):

> % nawk 'BEGIN{x="abc";split("abc",x)}'
> nawk: can't assign to x; it's an array name.

Yes, well, I never found the Solaris error messages spectacularly
illuminating =8-D

Quote:
>  source line number 1
> % gawk 'BEGIN{x="abc";split("abc",x)}'
> gawk: cmd. line:1: fatal: attempt to use scalar `x' as array
> %

This seems to agree with my understandng.

Quote:

> TAWK has no such problems.  Observe:

> % tawk 'BEGIN{x="abc";print typeof(x);split("abc",x);print typeof(x)}'
> string
> array
> %

I really wish that my employer would buy Tawk, but they won't

Regards,
Peter
--
Sent using an unregistered copy of RMRNews v1.02
Check out our website at http://www.rmrsoft.com/
for other high quality software for EPOC machines.



Wed, 14 Apr 2004 03:01:00 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)


Quote:



><snip>
>> >> I am curious as to why this is.  Why it doesn't work w/o that line.

>> >I guess that this is so that data is set to have array type, presumably
>> >otherwise it would be treated as a scalar.  This would be in keeping
>> >with awk's "once an array name, always an array name ..."

>> Yes, but it doesn't quite make sense - because the thing is used as an
>> array name later on.  I'm pretty sure (have lots of evidence for this, but

>Surely your gawk example below confirms my comment.  "x" has been given
>scalar type, the subsequent split() then barfs as a result.

You're right.  In GAWK, the "thing to avoid doing" is to use something as
an array after it has (somehow) acquired scalar type.

Quote:
>> can't quite specify it right now, off hand) that it (GAWK) scans the entire
>> source and figures out which things are arrays before executing anything.
>> So, it should know that data is an array, even though nothing in it has
>> been initialized yet.

>> BTW, I found this amusing (on a Solaris system):

>> % nawk 'BEGIN{x="abc";split("abc",x)}'
>> nawk: can't assign to x; it's an array name.

>Yes, well, I never found the Solaris error messages spectacularly
>illuminating =8-D

>>  source line number 1
>> % gawk 'BEGIN{x="abc";split("abc",x)}'
>> gawk: cmd. line:1: fatal: attempt to use scalar `x' as array
>> %

>This seems to agree with my understandng.

Yup - although I still think the compiler/interpreter of GAWK ought to have
figured out that it was an array from the context of the program.  The EAP
book contains a fair amount of text describing how GAWK figures out that
something is an array - generally, appearing in the program in an array
context ought to do it.

Quote:
>> TAWK has no such problems.  Observe:

>> % tawk 'BEGIN{x="abc";print typeof(x);split("abc",x);print typeof(x)}'
>> string
>> array
>> %

>I really wish that my employer would buy Tawk, but they won't

Bug them harder!
(That is, assuming you are using either DOS, OS2, Windows, or Solaris)


Wed, 14 Apr 2004 04:13:39 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

<snip>

Quote:
> >This seems to agree with my understandng.

> Yup - although I still think the compiler/interpreter of GAWK ought to
have
> figured out that it was an array from the context of the program.  The
EAP
> book contains a fair amount of text describing how GAWK figures out
that
> something is an array - generally, appearing in the program in an
array
> context ought to do it.

Finally managed to pursuade my daughter to let me use the computer - the
PDA's OK, but a bit fiddly when replying to emails & postings ;-)

What did you think of EAP3?  I really liked it and think that Aharon has
made a really useful reference for awk.

Of course there isn't really any reason, apart from history, why the
namespaces for arrays and scalars in awk shouldn't be disjoint.  It
would be perfectly possible to require the array name in split(), say,
to be followed by "[]" to indicate that it's in the array namespace (no
mucking about with funny symbols like perl please!).  I suspect that it
would be tricky to introduce this into existing code.  In addition even
if such syntax were introduced into gawk there are still the providers
of mawk, BWK's awk, MKS awk & all the other UNIX suppliers to convince,
not to mention POSIX.

Quote:
> >> TAWK has no such problems.  Observe:

> >> % tawk 'BEGIN{x="abc";print typeof(x);split("abc",x);print
typeof(x)}'
> >> string
> >> array
> >> %

Does tawk have separate namespaces for arrays and scalars?  That is
after the above split() code can you still reference the scalar x and
retrieve its value?  If not then I prefer the gawk implementation
because it at least warns you that the names are clashing.

Quote:

> >I really wish that my employer would buy Tawk, but they won't

> Bug them harder!
> (That is, assuming you are using either DOS, OS2, Windows, or Solaris)

I suspect the likely retort would be "your job is working on mainframe
systems, what are you doing mucking about with UNIX/DOS/....?" - and
regrettably I'd be forced into at least partial agreement.

Regards,
Peter
--
Peter S Tillier
"Who needs perl when you can write dc in sed?"
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Wed, 14 Apr 2004 05:32:32 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)


...

Quote:
>What did you think of EAP3?  I really liked it and think that Aharon has
>made a really useful reference for awk.

Good book - although mostly use it as a reference when debating trivial
minutiae of GAWK, such as in this thread...

Since I've become at least a small-time contibutor to the GAWK source code,
I figure I got to have the book.

Quote:
>Of course there isn't really any reason, apart from history, why the
>namespaces for arrays and scalars in awk shouldn't be disjoint.  It
>would be perfectly possible to require the array name in split(), say,
>to be followed by "[]" to indicate that it's in the array namespace (no
>mucking about with funny symbols like perl please!).  I suspect that it
>would be tricky to introduce this into existing code.  In addition even
>if such syntax were introduced into gawk there are still the providers
>of mawk, BWK's awk, MKS awk & all the other UNIX suppliers to convince,
>not to mention POSIX.

>> >> TAWK has no such problems.  Observe:

>> >> % tawk 'BEGIN{x="abc";print typeof(x);split("abc",x);print
>typeof(x)}'
>> >> string
>> >> array
>> >> %

>Does tawk have separate namespaces for arrays and scalars?  That is
>after the above split() code can you still reference the scalar x and
>retrieve its value?  If not then I prefer the gawk implementation
>because it at least warns you that the names are clashing.

No.  I think of TAWK as "object oriented", in the sense that names are just
pointers to objects - so "x" can be pointing to a scalar object one moment
and then to an array object the next.  And yes, when you switch it, the old
object is destroyed.  This may seem flaky to some, but it appeals to me.

There are actually 9 different values that "typeof()" can return.

Quote:

>> >I really wish that my employer would buy Tawk, but they won't

>> Bug them harder!
>> (That is, assuming you are using either DOS, OS2, Windows, or Solaris)

>I suspect the likely retort would be "your job is working on mainframe
>systems, what are you doing mucking about with UNIX/DOS/....?" - and
>regrettably I'd be forced into at least partial agreement.

They would seem to have a point there.


Wed, 14 Apr 2004 06:13:51 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

Quote:


> ...
> >What did you think of EAP3?  I really liked it and think that Aharon
has
> >made a really useful reference for awk.

> Good book - although mostly use it as a reference when debating
trivial
> minutiae of GAWK, such as in this thread...

> Since I've become at least a small-time contibutor to the GAWK source
code,
> I figure I got to have the book.

I think EaP3 and "sed and awk 2nd. ed." are very useful books and the
latter is among the most heavily thumbed in my collection - I'm fully
expecting EaP3 to become another.  There are lots of good, practical,
examples in these two books: this is not always the case with computer
books.  Another reference that I use a lot is Eric Pement's excellent
sed FAQ: when updating the www pages for the mainframe system that I
maintain I use sed, awk, or a combination to do all the hard work, but I
have to maintain them on UNIX boxes or Windows PCs.

Quote:
> >Of course there isn't really any reason, apart from history, why the
> >namespaces for arrays and scalars in awk shouldn't be disjoint.  It

<snip>

I'm currently looking at how to implement RS as RE in BWK's awk.  I have
ported it in the past to a couple of small memory/disk systems.  If the
implementation of RS as RE goes OK, then I'll take a look at the
namespace issue.

<snip>

Quote:
> >Does tawk have separate namespaces for arrays and scalars?  That is
> >after the above split() code can you still reference the scalar x and
> >retrieve its value?  If not then I prefer the gawk implementation
> >because it at least warns you that the names are clashing.

> No.  I think of TAWK as "object oriented", in the sense that names are
just
> pointers to objects - so "x" can be pointing to a scalar object one
moment
> and then to an array object the next.  And yes, when you switch it,
the old
> object is destroyed.  This may seem flaky to some, but it appeals to
me.

> There are actually 9 different values that "typeof()" can return.

I don't think it's flaky per se, but it would be useful if there was
some sort of warning that could be ignored (maybe a gawk-like "--lint"
option for example) where you were sure that you were happy to redefine
the "object".

On the whole it's a pity that awk can't, currently, differentiate
between scalar x, array x[] and function x() in a similar way to other
languages.  Still, I guess it's not too bad: at least we're not saddled
with the delightful syntax that PL/I supports:

    if then = else then then = if; else else = then; /* or similar */

[the positional syntax apart, PL/I is still one of my favourite
languages!]

Quote:
> >> Bug them harder!
> >> (That is, assuming you are using either DOS, OS2, Windows, or
Solaris)

> >I suspect the likely retort would be "your job is working on
mainframe
> >systems, what are you doing mucking about with UNIX/DOS/....?" - and
> >regrettably I'd be forced into at least partial agreement.

> They would seem to have a point there.

At the moment we don't use USS on the mainframes.  I can't really see
why this is so, after all with USS the machines are easier to use and I
could maintain my system's www pages on a _real_ web server using IBM's
sed and awk.  We could even use Linux on the beasts.

Regards,
Peter
--
Peter S Tillier
"Who needs perl when you can write dc in sed?"
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Wed, 14 Apr 2004 15:40:40 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

Quote:

> ...
> There are actually 9 different values that "typeof()" can return.

Just for intellectual curiosity, which ones are them?

--
  All true believers shall break their eggs at the convenient end.



Sun, 18 Apr 2004 00:00:42 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)


Quote:

>> ...
>> There are actually 9 different values that "typeof()" can return.

>Just for intellectual curiosity, which ones are them?

From page 192 of the "TAWK Compiler" manual for version 5.0:

        "int"                 integer
        "float"               floating point number
        "string"      string
        "array"               any type of array or table
        "regex"               a regular expression pattern
        "fileid"      a file descriptor from the fopen() function
        "unitialized" an unitialized variable (This variable will act
                        like a 0, or a "", depending on use.)
        "address"     the result of the addressof() function
        "unknown"     any other type



Sun, 18 Apr 2004 00:27:14 GMT  
 GAWK: Question on using filefuncs.so (creating arrays in extension functions)

Quote:



> ....
> >What did you think of EAP3?  I really liked it and think that Aharon has
> >made a really useful reference for awk.

> Good book - although mostly use it as a reference when debating trivial
> minutiae of GAWK, such as in this thread...

I finally tracked down the reference in EaP3 that covers the array vs.
scalar recognition issue.  It's p173, "Functions and Their Effects on
Variable Typing", with this code example:

function foo(a) {
    a[1] = 1 # parameter is an array

Quote:
}

BEGIN {
    b = 1
    foo(b)  # invalid: fatal type mismatch

    foo(x)  # x uninitialized, becomes an arry dynamically
    x = 1   # now not allowed, runtime error

Quote:
}

Regards,
Peter
--
To email direct use peter dot tillier at btinternet dot com
not the "reply to"
--
Sent using an unregistered copy of RMRNews v1.02
Check out our website at http://www.rmrsoft.com/
for other high quality software for EPOC machines.


Thu, 22 Apr 2004 20:44:22 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. creating a square wave using an array function

2. calling functions from gawk extensions

3. GAWK 3.0.95 - question on the extensions stuff

4. question about using array intrinsic functions

5. Creating Py Extensions Using Borland's Free Compiler v3.0

6. using Delphi to create REXX extensions

7. Creating Py Extensions Using Borland's Free Compiler v3.0

8. Creating Py Extensions Using Borland's Free Compiler

9. Creating Python Extensions Using Borland's Free Compiler

10. Tutorial on using NumPy arrays in C extension modules

11. C extension using Numeric arrays -- do I really need all this error

12. Property Assignment Syntax on Controls Made Using the Create() Built-in Function

 

 
Powered by phpBB® Forum Software