field value as array name? 
Author Message
 field value as array name?

wondering if the following is possible...

SQL query returns the following data:

Index_Value|Name1|Data1|Name2|Data2|Name3..Datax

Record length is variable
Field 1 is always index value
Field (2,3)..(x,x+1) are pairs

process this data with awk so that x is evaluated as an array name:

{ for (x=2; x<= NF-1; x+=2)
  { $(x)[$1] = ${x+1)
  }

Quote:
}

so:

1|First|John|Last|Doe
2|First|Bob|Last|Smith
1|Department|Accounting
2|Department|Sales
1|Address|123 Any Street|City|Anytown|State|AA

yields:

First[1]=John
First[2]=Bob
Last[1]=Doe
Last[2]=Smith
Department[1]=Accounting
Department[2]=Sales
Address[1]=123 Any Street
City[1]=Anytown
State[1]=AA



Sat, 06 Dec 2003 03:29:45 GMT  
 field value as array name?

Quote:

>wondering if the following is possible...

>SQL query returns the following data:

>Index_Value|Name1|Data1|Name2|Data2|Name3..Datax

>Record length is variable
>Field 1 is always index value
>Field (2,3)..(x,x+1) are pairs

>process this data with awk so that x is evaluated as an array name:

>{ for (x=2; x<= NF-1; x+=2)
>  { $(x)[$1] = ${x+1)
>  }
>}

>so:

>1|First|John|Last|Doe
>2|First|Bob|Last|Smith
>1|Department|Accounting
>2|Department|Sales
>1|Address|123 Any Street|City|Anytown|State|AA

>yields:

>First[1]=John
>First[2]=Bob
>Last[1]=Doe
>Last[2]=Smith
>Department[1]=Accounting
>Department[2]=Sales
>Address[1]=123 Any Street
>City[1]=Anytown
>State[1]=AA

You can't do this directly (AWK has no "eval" function), but you don't
really need to.  The usual method is to use a two-dimensional array (*)

So, in T-AWK, the method would be:

{
for (x=2; x<NF; x+=2)
    stuff[$1][$x] = $(x+1) # Note that I've reversed the order of your subs

Quote:
}

(*) Simulated in standard AWKs, exists natively in Thompson AWK

I've reversed the order of the subscripts, because Thompson allows you to
process arrays with the concept that "leftmost subscript is the most
significant".  That is, you could do, in an END clause:

END     {
        for (keyval in stuff)
            for (fieldname in stuff[keyva])
                print keyval,fieldname,stuff[keyval][fieldname]
        }



Sat, 06 Dec 2003 04:15:19 GMT  
 field value as array name?

Quote:

> wondering if the following is possible...

> SQL query returns the following data:

> Index_Value|Name1|Data1|Name2|Data2|Name3..Datax

> Record length is variable
> Field 1 is always index value
> Field (2,3)..(x,x+1) are pairs

> process this data with awk so that x is evaluated as an array name:

I don't think that this is possible, at least not with the standard
gawk/nawk/mawk (maybe TAWK).

Quote:
> { for (x=2; x<= NF-1; x+=2)
>  { $(x)[$1] = ${x+1)
>  }
> }

> so:

> 1|First|John|Last|Doe
> 2|First|Bob|Last|Smith
> 1|Department|Accounting
> 2|Department|Sales
> 1|Address|123 Any Street|City|Anytown|State|AA

> yields:

> First[1]=John
> First[2]=Bob
> Last[1]=Doe
> Last[2]=Smith
> Department[1]=Accounting
> Department[2]=Sales
> Address[1]=123 Any Street
> City[1]=Anytown
> State[1]=AA

At first glance this looks tempting, but would it be really that useful?
After you have read your arrays, you surely want to do something with
these arrays. Then you could either use the hardcoded names of the
arrays and e.g.

  print First[1]

- but that would mean you knew beforehand which array names could
possibly be in the file. If you don't know the possible attribute/array
names, what happens if the file suddenly starts to have lines with

1|ARGV|foo|ARGC|bar

? And still, even if the automatically created array names don't clash
with some other variables, you're stuck with the $(x)[i] notation
throughout your program. I don't think that's very much useful.

Alternatively, Awk has multidimensional1 arrays, so you can store e.g.

  A["First",1] = "John"
  A["First",2] = "Bob"
  A["Department",1] = "Accounting"

I'm sure you can easily write the code to fill your A array (I'm too
lazy at the moment). You can use another array which holds the attribute
names ("First","Last",...).

  for(i in A)
  {
    # i is (attribute_name SUBSEP index) here
    split(i,B,SUBSEP)  # split the string i into the array B
    print "attribute: " B[1] "  index:" B[2] "  value:" A[i]
  }

Regards...
                Michael

1: not truly multidimensional (search for SUBSEP in the man page), but
they work great in almost all cases and are very flexible.



Sat, 06 Dec 2003 04:41:52 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Copying one array value into subsequent array values

2. HowTo sort an Array not vor Names but for Values

3. Comparing value in an input field to any value from another file

4. Using field as value for another field

5. Field names in a field...

6. Selecting Fields Based On Field Name

7. field name and key name as a variable

8. field name and key name as a variable

9. array field as hot field

10. Field Applications Engineers: ASIC/Field Programable Gate Arrays.

11. [array names -inorder name]

12. Index Values of an Array inside a Cluster inside an Array

 

 
Powered by phpBB® Forum Software