associative arrays 
Author Message
 associative arrays

    Hi,

        I have been trying to understand the usage of associative array
now this i what i want to do

        input is

first a b c d a k l s w
second k z d f q w y u
third a b c d e a b c d e a b c d
fourth k z d f q w y u k z d f q w y u

    using nawk or gawk i want to store the elements in an associative
array so maybe

        {
                                    for (k = 2; k <= NF; k++) {
                        MyARRAY[$1, k] += 1;
                        }
        }

    So the array has

            MyARRAY[ "first", "a" ] = 2, MyARRAY[ "first","b" ] = 1 and
so on

    My question is how do i print this array out using the

        i,j in MyARRAY or other syntax without keeping the count of the
number of elements.

    Thanks,

    Saurabh



Sun, 28 Oct 2001 03:00:00 GMT  
 associative arrays


Quote:

> I have been trying to understand the usage of associative array
> now this i what i want to do

> input is

> first a b c d a k l s w
> second k z d f q w y u
> third a b c d e a b c d e a b c d
> fourth k z d f q w y u k z d f q w y u

> using nawk or gawk i want to store the elements in an associative
> array so maybe

> {
>   for (k = 2; k <= NF; k++) {
>     MyARRAY[$1, k] += 1;
>   }
> }

> So the array has

> MyARRAY[ "first", "a" ] = 2, MyARRAY[ "first","b" ] = 1 and so on

If this last line is what you want, your script doesn't do it. Try

{
  for (k = 2; k <= NF; k++) {
    MyARRAY[$1, $k] += 1;
  }

Quote:
}

where the only change is replacing MyARRAY[$1, k] with MyARRAY[$1, $k].
The original k second index is just the string representation of an
integer from 2 to NF. The revised $k is the k_th field.

Quote:
> My question is how do i print this array out using the i,j in MyARRAY
> or other syntax without keeping the count of the number of elements.

Array indexes in awk are _single_ strings rather than multiple numbers.
The index of array element A[1,2,3] is "1" SUBSEP "2" SUBSEP "3", so it
could also be referenced as A["1" SUBSEP "2" SUBSEP "3"]. Whenever awk
sees commas within square brackets, it effectively replaces the comma
with the awk system variable SUBSEP (which can be modified, but that's
usually a bad idea) and concatenates the result.

The simplest way to print out all elements of an array and their
indexes is

for (i in MyARRAY) print i, MyARRAY[i]

The resulting order isn't predictable because the method used to
implement associative arrays can differ from one awk to another. One
awk could implement them using hash tables, and other could use a
singly linked list.

If you want to control the order in which array elements are printed,
you'll need to store the indexes. Associative arrays make this possible
as well with little effort.

{
  if (!($1 in Indx)) Indx[++Indx[0]] = Indx[$1] = $1
  for (k = 2; k <= NF; ++k) {
    if (!($1 SUBSEP $k in Indx))
      Indx[$1,++Indx[$1,0]] = Indx[$1,$k] = $k
    ++MyARRAY[$1,$k]
  }

Quote:
}

END {
  for (i = 1; i <= Indx[0]; ++i) {
    ii = Indx[i]
    for (j = 1; j <= Indx[ii,0]; ++j) {
      jj = Indx[ii,j]
      printf("[%s,%s]\t%d\n", ii, jj, MyARRAY[ii,jj])
    }
  }

Quote:
}

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---


Sun, 28 Oct 2001 03:00:00 GMT  
 associative arrays
I will use getline fuction for loading associative arrays. In END section
use for loop i.e for(element in array) {print array}. Check any book on
that for syntax.

Anyway just a thought.

Sanjeev K. Sagar

Quote:

>     Hi,

>         I have been trying to understand the usage of associative array
> now this i what i want to do

>         input is

> first a b c d a k l s w
> second k z d f q w y u
> third a b c d e a b c d e a b c d
> fourth k z d f q w y u k z d f q w y u

>     using nawk or gawk i want to store the elements in an associative
> array so maybe

>         {
>                                     for (k = 2; k <= NF; k++) {
>                         MyARRAY[$1, k] += 1;
>                         }
>         }

>     So the array has

>             MyARRAY[ "first", "a" ] = 2, MyARRAY[ "first","b" ] = 1 and
> so on

>     My question is how do i print this array out using the

>         i,j in MyARRAY or other syntax without keeping the count of the
> number of elements.

>     Thanks,

>     Saurabh



Mon, 29 Oct 2001 03:00:00 GMT  
 associative arrays
On Wed, 12 May 1999 12:03:42 -0500, "Chadha, Saurabh (BNR:RICH2:2N64)"

Quote:

>    Hi,

>        I have been trying to understand the usage of associative array
>now this i what i want to do

>        input is

>first a b c d a k l s w
>second k z d f q w y u
>third a b c d e a b c d e a b c d
>fourth k z d f q w y u k z d f q w y u

>    using nawk or gawk i want to store the elements in an associative
>array so maybe

>        {
>                                    for (k = 2; k <= NF; k++) {
>                        MyARRAY[$1, k] += 1;
>                        }
>        }

>    So the array has

>            MyARRAY[ "first", "a" ] = 2, MyARRAY[ "first","b" ] = 1 and
>so on

>    My question is how do i print this array out using the

>        i,j in MyARRAY or other syntax without keeping the count of the
>number of elements.

The syntax is like this
  if ( (i,j) in myarray ) print myarray[i,j]

--
Steve



Mon, 29 Oct 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. associative arrays in J?

2. Associative Array Performance

3. Associative Arrays

4. associative arrays/memory

5. Associative Array with PC AWK

6. Associative arrays

7. Associative Arrays

8. Associative Arrays

9. associative arrays (was: Perl)

10. Are Associative Arrays unique to Perl?

11. Associative Arrays

12. Associative arrays part 2

 

 
Powered by phpBB® Forum Software