2D Array to display characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2D Array to display characters
# 1  
Old 02-27-2008
2D Array to display characters

Hi All,

I have a series of number which indicates the coordinates
where 0104 indicates X-coord = 01 and Y-coord = 04 and i want characters to form up in accordance to the coordinates given.
Can any expert give me an example of 2D array code using csh or awk or perl such that the code can display the characters below ?

EG:
Inputfile:
0101 A
0102 B
0103 C
0104 D
0201 E
0202 F
0203 G
0204 H
0301 I
0302 J
0303 K
0304 L

Code:
04  D   H   L 
03  C   G   K
02  B   F   J
01  A   E   I  
   01  02  03


Last edited by Raynon; 02-27-2008 at 07:26 AM..
# 2  
Old 02-27-2008
This way:
Code:
awk '{
X[$2]=substr($1,1,2)
Y[$2]=substr($1,3)
}
END {
for ( y in Y)
   for ( x in X )
      if ( x == y  )
        {
        dx=sprintf("%d",X[x])
        dy=sprintf("%d",Y[y])
        if ( dx >= max_x )
           max_x=dx
        if ( dy >= max_y )
           max_y=dy
        f[dx,dy]=x 
        }
for (i=max_y;i>=1;i--)
    {
    printf("%2d",i)
    for (j=1;j<=max_x;j++)
       printf(" %2s",f[j,i])
    printf("\n")
    }
printf("   ")
for (j=1;j<=max_x;j++)
   printf("%2d ",j)
print
} ' file
 4  D  H  L
 3  C  G  K
 2  B  F  J
 1  A  E  I
    1  2  3

# 3  
Old 02-27-2008
Hi Klashxx,

Your code is really cool. Thks , it works up till a 9x9 matrix.
However, I tried the code with x-coordinates 10 and above and it doesn't work. Can you help ?

EG:
Inputfile:
0101 A
0102 B
0103 C
0104 D
0201 E
0202 F
0203 G
0204 H
0301 I
0302 J
0303 K
0304 L
1001 M
1002 N
# 4  
Old 02-27-2008
Ok, it was a conversion problem, so:
Code:
> cat file
0101 A
0102 B
0103 C
0104 D
0201 E
0202 F
0203 G
0204 H
0301 I
0302 J
0303 K
0302 L
0903 U
1001 M
1002 N
1104 Ñ
1504 F
2005 N

Code:
>awk '{
X[$2]=substr($1,1,2)
Y[$2]=substr($1,3)
}
END {
for ( y in Y)
   for ( x in X )
      if ( x == y  )
        {
        dx=X[x]+0
        dy=Y[y]+0
        if ( dx >= max_x )
           max_x=dx
        if ( dy >= max_y )
           max_y=dy
        f[dx,dy]=x 
        }
for (i=max_y;i>=1;i--)
    {
    printf("%.2d",i)
    for (j=1;j<=max_x;j++)
       printf(" %2s",f[j,i])
    printf("\n")
    }
printf("   ")
for (j=1;j<=max_x;j++)
   printf("%.2d ",j)
print
} ' file
05                                                           N
04  D  H                          Ñ           F               
03  C  G  K                 U                                 
02  B     L                                                   
01  A  E  I                    M                              
   01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20

Bye

Last edited by Klashxx; 02-27-2008 at 10:25 AM.. Reason: bug solved
# 5  
Old 02-27-2008
Hi Klashxx,

Strange, i tried to use yr modified code but some strange characters appear at the end of output in blue. Can you give some advice on that

Code:
$ cat input1
0101 A
0102 B
0103 C
0104 D
0201 E
0202 F
0203 G
0204 H
0301 I
0302 J
0303 K
0302 L
0801 Z
0903 U
1001 M
1002 N

$ awk -f 2d_array input1
04  D  H
03  C  G  K                 U
02  B  F  L                    N
01  A  E  I              Z     M
   01 02 03 04 05 06 07 08 09 10 1002 N

# 6  
Old 02-27-2008
Hi , change last print for printf("\n")
# 7  
Old 02-27-2008
Hi Klashxx,

Thks a million!!!!. It works!!
Thats real cool!!.
But i am a beginner in awk, can you explain your code such that i can comprehend better. Especially the looping part, which seems confusing to me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

2. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

3. UNIX for Dummies Questions & Answers

Display only the first two characters of all the lines from a file.

how do i Display only the first two characters of all the lines from a file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

4. Shell Programming and Scripting

Display first few characters only

I have a file with one or two lengthy lines like.... masterUserID = 'S' OR masterUserID = 'S' OR masterUserID = 'S' (many many more times) How do I display only the first few hundred characters from each line. (1 Reply)
Discussion started by: shantanuo
1 Replies

5. UNIX and Linux Applications

Display non-english characters in shell

I don't know if this is the correct thread to post. If not tell me please. I have a script to make some queries to a Spanish Dictionay (Real Academia Española) as follows: lynx -dump --nolist "buscon.rae.es/draeI/SrvltGUIBusUsual?TIPO_ HTML=2&LEMA="$1 It works this way: ./rae word ... (1 Reply)
Discussion started by: agasamapetilon
1 Replies

6. UNIX for Dummies Questions & Answers

Display the contents of an array

Hi i have just registered So i am at university studying forensic computing and we have to learn c++ i have never done anything with c++ before and i am abit stuck i need to create a programme to display the contents of an array of characters forwards and in reverse Can anyone help me... (1 Reply)
Discussion started by: RossMc
1 Replies

7. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

8. Shell Programming and Scripting

Display special characters

I have a file that evidently has some special characters in it. Is there a Unix command that I can use tp display the file so I can see the octal or hex values? (2 Replies)
Discussion started by: BCarlson
2 Replies

9. UNIX for Dummies Questions & Answers

Garbage characters in display

:confused: I recently left managment and came back to AIX administration. So I am a bit rusty to say the least. The issue I am having is with the term settings when I either simply telnet to my AIX unit or even when I use an emulator like puTTY or a VAR provided one. Once I am logged into... (0 Replies)
Discussion started by: Haleja001
0 Replies

10. AIX

Display EBCDIC as Characters

Hi, Does anybody have a command to share that will display EBCDIC files as character? I have od -x -A d which will display hex as follows f0f1 f4f3 f0f0 f0f0 f0f6 f1f4 f6f3 f3f0 f3f9 f8f3 f1f6 f8f2 f860 d1e4 d360 f2f0 f0f5 4000 0000 5014 8c00 0000 0000 0c00 0000 0000 0d00 0000 5014 8c00... (1 Reply)
Discussion started by: LouPelagalli
1 Replies
Login or Register to Ask a Question