Accessing single elements of a awk array in END


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing single elements of a awk array in END
# 1  
Old 06-20-2008
Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below?

Code:
tst[Var,"-"VarC]=sprintf("%5.2f",Car[BCar] / 12)

When I scan thru the array with

Code:
for ( i in tst ) { print i,tst[i] }

I get the output of:

Code:
vec-7 144

But when I try this in the END

Code:
print tst[vec-7]

It looks like it's not set.

What am I doing wrong?? Something with the SUBSEP???

Please help.

Thanks
# 2  
Old 06-20-2008
Without quotes, "vec-7" is evaluated as "value of variable vec" minus "7", which isn't a good key for your hash.

Try this:
Code:
print tst["vec-7"]

# 3  
Old 06-20-2008
still not getting it. Do I need to assign indices differently, maybe???
# 4  
Old 06-20-2008
I got it.
Thanks A LOT robotronic. I changed:

Code:
tst[Var,"-"VarC]=sprintf("%5.2f",Car[BCar] / 12)

to

Code:
tst[Var"-"VarC]=sprintf("%5.2f",Car[BCar] / 12)

Then I did your code to print it with the quotes. I removed the ","

Thanks again, I was getting frustraded.
# 5  
Old 06-20-2008
What about:

Code:
print tst["vec","-7"]

In awk an associative array could have only one dimension so, if you assign your keys with the syntax:

Code:
tst[Var,"-"VarC]= ...

The resulting key of the one-dimensional hash will be the concatenation between two values but, for some reason, it seems that you must always use "," to express the key.

To avoid problems, you should remove the comma in the key value, assigning the value in a way like this:

Code:
tst[Var"-"VarC]

Try and see Smilie
# 6  
Old 06-20-2008
Oh, I see that you found the solution by yourself while I was writing Smilie

Good Smilie
# 7  
Old 06-20-2008
Thanks again, I found it with your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

2. Shell Programming and Scripting

Problem accessing array elements

Hi all, I can’t resolve an array element access issue on (Linux/pdksh) .. So I’m positing for advice.By the way - a friend demonstrated to me - same script works as expected under Solaris. I have been working on a documentation project where many *.jpg screen shots are used in the... (4 Replies)
Discussion started by: njdpo
4 Replies

3. Shell Programming and Scripting

Match elements in an AWK multi-dimensional array

Hello, I have two files in the following format; file1: A B C D E F G H I J K L file2: 1 2 3 4 5 6 7 8 9 10 11 12 I have read them both in to multi-dimensional arrays. I need a file that has column 2 of the first file printed out for each column 3 of the second file ie... ... (3 Replies)
Discussion started by: cold_Que
3 Replies

4. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

5. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=rthg34 n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values. in the below if... (2 Replies)
Discussion started by: usustarr
2 Replies

6. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

7. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

8. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

9. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

10. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies
Login or Register to Ask a Question