How big is my array? 
Author Message
 How big is my array?

Is there an awk internal function which returns the number of items in an
array, something like

length (ArrayName)

or do I have to count them myself, like

for (i in ArrayName)
    count += 1

? This would be "nice to have" if it's not there already.

Thanks in advance for your help.

--
Alan Mackenzie (Munich, Germany)

(like "aa"), remove one of them (leaving, say, "a").



Sat, 19 Apr 2003 01:15:42 GMT  
 How big is my array?

Quote:

>Is there an awk internal function which returns the number of items in an
>array, something like

>length (ArrayName)

>or do I have to count them myself, like

>for (i in ArrayName)
>    count += 1

>? This would be "nice to have" if it's not there already.

Thompson AWK has this feature (working just the way you describe).

If you're not using T-AWK, you should be.

http://www.tasoft.com



Sat, 19 Apr 2003 01:54:06 GMT  
 How big is my array?

11:54:06 -0600:

Quote:


>>Is there an awk internal function which returns the number of items in
>>an array, something like
>>length (ArrayName)
>>or do I have to count them myself, like
>>for (i in ArrayName)
>>    count += 1
>>? This would be "nice to have" if it's not there already.

Gosh, that was quick off the mark!

Quote:
> Thompson AWK has this feature (working just the way you describe).

Really? I had no idea!

Quote:
> If you're not using T-AWK, you should be.

I'm using gawk. I'll try downloading tawk and see what it has. I take it
that the URL below is for downloading tawk. I need whichever awk to run
under Windows NT, :-( so I'll see if tawk has this as well.

Cheers!

Quote:
> http://www.tasoft.com

--
Alan Mackenzie (Munich, Germany)

(like "aa"), remove one of them (leaving, say, "a").


Sat, 19 Apr 2003 02:11:09 GMT  
 How big is my array?

Quote:


>11:54:06 -0600:
...
>I'm using gawk. I'll try downloading tawk and see what it has. I take it
>that the URL below is for downloading tawk. I need whichever awk to run
>under Windows NT, :-( so I'll see if tawk has this as well.

The good news is that T-AWK was originally developed under DOS and enhanced
to work with Windows - you can actually access the Win32 API from your AWK
programs!

The bad news is that it isn't freeware or shareware.

The good news is that it is worth every penny - I haven't even begun to
scratch the surface of the nice things it has.



Sat, 19 Apr 2003 03:59:58 GMT  
 How big is my array?
On Mon, 30 Oct 2000 17:15:42 +0000, Alan

Quote:

>Is there an awk internal function which returns the
>number of items in an array, something like

>length (ArrayName)

Not that I know of.

Quote:
>or do I have to count them myself, like

>for (i in ArrayName)
>    count += 1

Well, you could keep a counter while you are populating the
array with elements. If you build an array with split() you can
use split's return value, as in :

NrOfElements = split("abc;def;ghi",tmp,";")

Quote:
>? This would be "nice to have" if it's not there already.

Of course you can turn your example into a function in your awk
script itself or in your library of standard functions :

function NrOfElements(arr     ,_index,_count){
        _count = 0;
        for (_index in arr) _count++;
        return _count

Quote:
}

and use it in your scripts :

split("abc;def;ghi;jkl;mno;pqr;stu",myarray,";")
print "There are " NrOfElements(myarray) " elements in my array"

which produces: There are 7 elements in my array

Have fun!

--
  (  Kees Nuyt; Rotterdam; Netherlands

c[_] Disclaimer: Any opinions etc. are mine, not necessarily my employer's.



Sat, 19 Apr 2003 05:02:48 GMT  
 How big is my array?

says...

Quote:



>>11:54:06 -0600:
>...
>>I'm using gawk. I'll try downloading tawk and see what it has. I take it
>>that the URL below is for downloading tawk. I need whichever awk to run
>>under Windows NT, :-( so I'll see if tawk has this as well.

>The good news is that T-AWK was originally developed under DOS and enhanced
>to work with Windows - you can actually access the Win32 API from your AWK
>programs!

>The bad news is that it isn't freeware or shareware.

>The good news is that it is worth every penny - I haven't even begun to
>scratch the surface of the nice things it has.

... and the bad news is that if your want your awk scripts to be freely portable
to other platforms and usable by most of the awk community, don't use the
non-standard features of tawk; use gawk.


Sat, 19 Apr 2003 04:54:31 GMT  
 How big is my array?


Quote:
>On Mon, 30 Oct 2000 17:15:42 +0000, Alan

>>Is there an awk internal function which returns the
>>number of items in an array, something like

>>length (ArrayName)

>Not that I know of.

>>or do I have to count them myself, like

>>for (i in ArrayName)
>>    count += 1

>Well, you could keep a counter while you are populating the
>array with elements. If you build an array with split() you can
>use split's return value, as in :

>NrOfElements = split("abc;def;ghi",tmp,";")

<snip>

Also depends on how you're populating the array. If doing so with
split(), then do as recommended above - use split()'s return value. If
reading in array entries from stdin or redirected input, keep track of
the number in situ:

some_condition {
  if (!($1 in some_array)) {
    some_array[$1] = $2
    ++some_array[""]
  }

Quote:
}

Well, that's the convention I use. "" is a valid array index and usually
not a valid input field. If "" were a valid input field value, something
like ++some_array["", ""], equivalently, ++some_array[SUBSEP] could
work.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 19 Apr 2003 10:03:26 GMT  
 How big is my array?
...

Quote:
>>The good news is that it is worth every penny - I haven't even begun to
>>scratch the surface of the nice things it has.

>... and the bad news is that if your want your awk scripts to be freely
>portable to other platforms and usable by most of the awk community,
>don't use the non-standard features of tawk; use gawk.

Funny you should mention that...  These days, about the only time I use GAWK
is when I need to use one or both of the following features:
        1) Built-in networking (really handy, that!)
        2) FIELDWIDTHS

Standard stuff...  (Beyond that, I have no idea what your point is)



Sat, 19 Apr 2003 10:21:57 GMT  
 How big is my array?
On Mon 30 Oct 2000 09:21:57p,  In article

Quote:



>>>The good news is that it is worth every penny - I haven't even begun
>>>to scratch the surface of the nice things it has.

>>... and the bad news is that if your want your awk scripts to be freely
>>portable to other platforms and usable by most of the awk community,
>>don't use the non-standard features of tawk; use gawk.

>Funny you should mention that...  These days, about the only time I use
>GAWK is when I need to use one or both of the following features:
>     1) Built-in networking (really handy, that!)
>     2) FIELDWIDTHS

>Standard stuff...  (Beyond that, I have no idea what your point is)

His point is simply this: if you write write awk scripts using the "nice
things" that only tawk has, then those scripts will not run under gawk.

And so your awk scripts will not be portable to (the many) platforms that
tawk does not support.  

Is that clear enough?



Sat, 19 Apr 2003 10:55:38 GMT  
 How big is my array?

Quote:

> Is there an awk internal function which returns the number of items in
> an array, something like

> length (ArrayName)

No.

Quote:
> or do I have to count them myself, like

> for (i in ArrayName)
>     count += 1

> ?

Yes. But use the increment operator:

    count++

As Kees Nuyt pointed out, if you create the array using the built-in
function split(), then the size of the resultant array is given by
the return value of the function.

Do NOT use an aggregate variable to store a simple scalar value.
In particular, do not store the size of the array in an element of
the array itself. This would needlessly ruin the homogeneousness
of the data structure and cause you to have to repeatedly treat
the size-of-array element as a "special case" each time you iterate
the elements of the array. Pointless.

Quote:
> This would be "nice to have" if it's not there already.

Larry Wall thought so too, so in the '80s he added it to awk along
with some other "nice to have" stuff and called it Perl:


    $count = keys %Hash; # $count is now size of associative array %Hash

Like GNU awk, Perl is ubiquitous and free (freely available and
free of charge).

--
Jim Monty

Tempe, Arizona USA



Sun, 20 Apr 2003 01:21:20 GMT  
 How big is my array?

Quote:

>> Is there an awk internal function which returns the number of items in
>> an array, something like
>> length (ArrayName)
> No.

Thanks to all the people who told me "no" :-). I sometimes find it a
shame that authors of introductory books never find it in themselves to
write things like "there is no built in method for getting the number of
elements in an array", or "the C bitwise operators are not present in
awk."

Quote:
>> or do I have to count them myself, like

>> for (i in ArrayName)
>>     count += 1

>> ?
> Yes. But use the increment operator:
>     count++

Why?  Does it make any difference in awk? As a windblown C programmer, a
bare expression looks a bit, well, bare to me, while the assignment
somehow looks more complete. (Yeah, I know it's also still an expression,
...).

Quote:
> As Kees Nuyt pointed out, if you create the array using the built-in
> function split(), then the size of the resultant array is given by
> the return value of the function.

Yes, I had indeed created the pertinent array using split, and yes, I
could do with reading the fine manual properly, sometimes. Thanks again
to you and Kees.

Quote:
> Do NOT use an aggregate variable to store a simple scalar value.
> In particular, do not store the size of the array in an element of
> the array itself. .....

Hmmm. That sounds kind of familiar... "Their are four misstakes in this
sentense.", or "This set contains exactly those sets which aren't
members of themselves."  It kind of wouldn't make sense.

Quote:
> .... This would needlessly ruin the homogeneousness of the data
> structure and cause you to have to repeatedly treat the size-of-array
> element as a "special case" each time you iterate the elements of the
> array. Pointless.

Yep, agreed. It disturbs me how many software "engineers" seem unaware of
such basic principles of programming hygiene, and it disturbs me even
more when I have to modify their code. :-(

Quote:
>> This would be "nice to have" if it's not there already.
> Larry Wall thought so too, so in the '80s he added it to awk along
> with some other "nice to have" stuff and called it Perl:

>     $count = keys %Hash; # $count is now size of associative array %Hash

Ah yes, perl. I never realised that there was a religious war between
awkers and perl-fishers until I subscribed to comp.lang.awk. Is there
really one?

perl seems kind of overcomplicated to learn (for somebody as lazy as me),
when awk will do the job (not that it always will). A bit like PL/1
compared with fortran, I suppose.

Quote:
> Like GNU awk, Perl is ubiquitous and free (freely available and
> free of charge).

Yes, I suppose I should think about adding length(ArrayName) into gawk
myself.

Quote:
> Jim Monty

--
Alan Mackenzie (Munich, Germany)

(like "aa"), remove one of them (leaving, say, "a").


Sun, 20 Apr 2003 03:43:44 GMT  
 How big is my array?

Quote:

>Thanks to all the people who told me "no" :-). I sometimes find it a
>shame that authors of introductory books never find it in themselves to
>write things like "there is no built in method for getting the number of
>elements in an array", or "the C bitwise operators are not present in
>awk."

Well gee, if we wrote about all the things that aren't in awk, we'd
never be done, would we?  Ada packages, C++ classes, etc... If you
hit the end of the book and didn't see a feature listed, then it's a
good bet it's not there.

Quote:
>>> or do I have to count them myself, like

>>> for (i in ArrayName)
>>>     count += 1
>> Yes. But use the increment operator:

>>     count++
>Why?  Does it make any difference in awk?

Absolutely none.  All math is done using double precision floating point.
Write it the way you like.

Quote:
>Ah yes, perl. I never realised that there was a religious war between
>awkers and perl-fishers until I subscribed to comp.lang.awk. Is there
>really one?

It's probably better if I don't comment publicly on this...
I'll just say what I've said before, if you want perl, you know where to
get it.

Quote:
>Yes, I suppose I should think about adding length(ArrayName) into gawk
>myself.

Happy hacking. That's the point of Free Software.  Enjoy.

Arnold
--
Arnold Robbins                  | If There's More Than One Way To Do It,
Guest account at Emory Math/CS  | then why is there only one implementation

                                |       -- Arnold Robbins



Sun, 20 Apr 2003 23:23:09 GMT  
 How big is my array?

-0500:

Quote:


>>Thanks to all the people who told me "no" :-). I sometimes find it a
>>shame that authors of introductory books never find it in themselves to
>>write things like "there is no built in method for getting the number
>>of elements in an array", or "the C bitwise operators are not present
>>in awk."
> Well gee, if we wrote about all the things that aren't in awk, we'd
> never be done, would we?  Ada packages, C++ classes, etc... If you
> hit the end of the book and didn't see a feature listed, then it's a
> good bet it's not there.

OK, fair point! Maybe it's just me, but somehow I would expect to be able
to count an array for free, it sort of seems to be a such a natural thing
to want to do.  But perhaps most arrays do come from split(), in which
case you've got the size anyhow, or they're created one element at a time
(like a symbol table, for example), in which case you can count as you go
along.

But I wouldn't really be expecting C++ classes; it'd overload the
language with something which would make what is now a short sharp
program an utter drag to write. If I want C++, I know where to find it!
All in my humble opinion, of course.

Quote:
> All math is done using double precision floating point.  Write it the
> way you like.

Eueurgghhh!  That's one thing I'm glad I didn't find in the manual. :-)
Maybe that has something to do with the absence of bit-operators.

[ .... ]

Quote:
>>Yes, I suppose I should think about adding length(ArrayName) into gawk
>>myself.
> Happy hacking. That's the point of Free Software.  Enjoy.

Well, I'm just leafing through awk.y and builtin.c at the moment. Who
knows?

By the way, thanks for doing gawk. It's a bundle of fun to use, even if I
haven't quite got the tricky bits sussed out yet.

Quote:
> Arnold

--
Alan Mackenzie (Munich, Germany)

(like "aa"), remove one of them (leaving, say, "a").


Mon, 21 Apr 2003 01:19:05 GMT  
 How big is my array?

Quote:


...
>>Do NOT use an aggregate variable to store a simple scalar value.
>>In particular, do not store the size of the array in an element of
>>the array itself. .....

>Hmmm. That sounds kind of familiar... "Their are four misstakes in this
>sentense.", or "This set contains exactly those sets which aren't
>members of themselves."  It kind of wouldn't make sense.

>>.... This would needlessly ruin the homogeneousness of the data
>>structure and cause you to have to repeatedly treat the size-of-array
>>element as a "special case" each time you iterate the elements of the
>>array. Pointless.

>Yep, agreed. It disturbs me how many software "engineers" seem unaware
>of such basic principles of programming hygiene, and it disturbs me
>even more when I have to modify their code. :-(

Yes, it is a dirty practice, but how 'dirty' is this? If you want to
use 'for (i in myarray)', it's a bit of a pain to have to have to add
a 'if (i !~ excludedpatterns)' to it. For small data sets, it's not
worth it. Direct enumeration of array elements is quick enough and much
cleaner. For large to huge datasets, however, direct enumeration can
become mighty slow. You could use separate variables like count_myarray
to maintain the number of elements of myarray, but this adds the
possibility of typo bugs.

Putting the meta data into the array encapsulates it with its data. If
you believe awk arrays should always be pure set relations, then this
would be a bad thing. On the other hand, it allows some crude OOP-like
functionality, and arrays are the only device with which to do this in
awk, since they're the only mechanism to bind different values into a
single variable. And if 'for (i in myarray) if (i !~ "^" SUBSEP) ...'
were a performance drag, it could be eliminated like so:

n = myarray[SUBSEP]
delete myarray[SUBSEP]
for (i in myarray) do_something
myarray[SUBSEP] = n

But in the final analysis, it's a question of personal preference.

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 21 Apr 2003 03:57:49 GMT  
 How big is my array?

Quote:



>>>Do NOT use an aggregate variable to store a simple scalar value.
>>>In particular, do not store the size of the array in an element of
>>>the array itself. .....
>>Hmmm. That sounds kind of familiar... "Their are four misstakes in this
>>sentense.", or "This set contains exactly those sets which aren't
>>members of themselves."  It kind of wouldn't make sense.
>>>.... This would needlessly ruin the homogeneousness of the data
>>>structure and cause you to have to repeatedly treat the size-of-array
>>>element as a "special case" each time you iterate the elements of the
>>>array. Pointless.
>>Yep, agreed. It disturbs me how many software "engineers" seem unaware
>>of such basic principles of programming hygiene, and it disturbs me
>>even more when I have to modify their code. :-(
> Yes, it is a dirty practice, but how 'dirty' is this? If you want to
> use 'for (i in myarray)', it's a bit of a pain to have to have to add a
> 'if (i !~ excludedpatterns)' to it. For small data sets, it's not worth
> it. Direct enumeration of array elements is quick enough and much
> cleaner. For large to huge datasets, however, direct enumeration can
> become mighty slow.

You mean something like 'for (i in array) count += 1'?

Quote:
> You could use separate variables like count_myarray to maintain the
> number of elements of myarray, but this adds the possibility of typo
> bugs.

Typically an array is made an element at a time, or comes from a split
operation, so keeping track of its size shouldn't be a problem. (Yes, I
know that it was me that raised it as a problem in the first place :-)

Quote:
> Putting the meta data into the array encapsulates it with its data. If
> you believe awk arrays should always be pure set relations, then this
> would be a bad thing. On the other hand, it allows some crude OOP-like
> functionality, and arrays are the only device with which to do this in
> awk, since they're the only mechanism to bind different values into a
> single variable. And if 'for (i in myarray) if (i !~ "^" SUBSEP) ...'
> were a performance drag, it could be eliminated like so:

> n = myarray[SUBSEP]
> delete myarray[SUBSEP]
> for (i in myarray) do_something
> myarray[SUBSEP] = n
> But in the final analysis, it's a question of personal preference.

I've been thinking about this for some while. I still feel that putting
the count itself inside the array it's counting _is_ a Bad Thing. OOP
thingies are mechanisms for efficiently writing BIG programs, whereas awk
is a language optimised for small programs, and to "enhance" it with OOP
features would make it less useful for its main thing.

--
Alan Mackenzie (Munich, Germany)

(like "aa"), remove one of them (leaving, say, "a").



Tue, 06 May 2003 03:00:00 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. big arrays cause big object files in g77

2. Syntax error: Array too big ( CW4b)

3. Array too big??

4. Array too big, Part 2

5. S3 on NT4.0: Problem with big Arrays

6. Really big Arrays with max 64k data objects

7. Initializing big array

8. Big arrays in COM

9. Problem with defining *BIG* arrays using Verdix Ada on Apollo - Help Please

10. Improve efficiency of 3 big array ops.

11. Biggest array

12. Big array indexes (Was: Integer types for 128-bit addressing)

 

 
Powered by phpBB® Forum Software