2D Array to display characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2D Array to display characters
# 8  
Old 02-28-2008
Here u have the new commented version (new feature added):
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
1610 U
1104 Ñ
1504 F
2005 N

Code:
>awk '{
# Associative arrays to store the coordinates of each element
# the source file could have duplicates so we add the Number of Record (NR)
# variable to $2 in order to get uniques items. 
X[$2""NR]=substr($1,1,2) 
Y[$2""NR]=substr($1,3)
}
END {
for ( y in Y)                   #BEGIN First loop:go through Y  
   for ( x in X )               #BEGIN Second loop:for each item of Y go through X 
      if ( x == y  )            #BEGIN if
        {                       #if both  elements of X and Y have the same index ($2+NR in the source file)
        dx=X[x]+0               #Store x value and add 0 to convert it to integer
        dy=Y[y]+0               #Store y value and add 0 to convert it to integer
        if ( dx >= max_x )     
           max_x=dx             #Is this the maximum x value ?
        if ( dy >= max_y )
           max_y=dy             #Is this the maximum y value ?
        f[dx,dy]=substr(x,1,1)  #Store the result in a 2D array and get rid of the NR part  
        break                   #Cause each element is unique ,dont need to look for another "x==y" so break second loop
        }                       #END if
                                #END Second loop
                                #END First loop
 
# SHOW RESULTS (i ll leave the presentation code for you)        
        
for (y=max_y;y>=1;y--)          #BEGIN coordinate y loop
    {
    printf("%.2d|",y)
    for (x=1;x<=max_x;x++)      #BEGIN coordinate x loop
       printf(" %2s|",f[x,y])   #END coordinate x loop --> Just print in the correct place the item. 
    printf("\n")
    for (x=1;x<max_x+2;x++)
       printf("----")
    printf("\n")
    }                           #END coordinate y loop         
printf("  ")
for (x=1;x<=max_x;x++)
   printf("|%.2d ",x)
printf("|\n")
} ' file
10|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |  U|   |   |   |   |
------------------------------------------------------------------------------------
09|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
08|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
07|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
06|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
05|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |  N|
------------------------------------------------------------------------------------
04|  D|  H|   |   |   |   |   |   |   |   |  Ñ|   |   |   |  F|   |   |   |   |   |
------------------------------------------------------------------------------------
03|  C|  G|  K|   |   |   |   |   |  U|   |   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
02|  B|  F|  J|   |   |   |   |   |   |  N|   |   |   |   |   |   |   |   |   |   |
------------------------------------------------------------------------------------
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 |

Cheers
# 9  
Old 02-28-2008
Wow Klashxx!! That code of your was spectacular !!
And thanks for the comprehensive explanation.
I have definitely learnt alot out of this.

Just a couple of question:
1) If the character to be displayed sometimes contain a maximum of 2 characters (number of characters will not exceed 2), how can you modify your code to suit this requirement ?
EG:
0101 AA
0102 B
0103 CD
0104 D
0201 EG
0202 F
0203 G
0204 HH

2) If the coordinates is not displayed in the input file , eg 0202 is not provided here, can you allow your code to print a minus character " - " when this situation arise.

EG:
0101 A
0102 B
0201 C

Expected output:
Code:
02  B  -
01  A  C
   01 02

# 10  
Old 02-28-2008
This way:
Code:
> cat file
0101 A
0102 BH
0103 C
0104 D
0201 E
0202 F
0203 G
0204 HG
0301 I
0302 LZ
0303 K
0903 U
1001 M
1002 NX
1610 U
1405 GH
1104 Ñ
1504 F
1902 LL
2005 N

Code:
>awk '{
# Associative arrays to store the coordinates of each element
# the source file could have duplicates so we add the Number of Record (NR)
# variable to $2 in order to get uniques items. 
X[$2""NR]=substr($1,1,2) 
Y[$2""NR]=substr($1,3)
}
END {
for ( y in Y)                   #BEGIN First loop:go through Y  
   for ( x in X )               #BEGIN Second loop:for each item of Y go through X 
      if ( x == y  )            #BEGIN if
        {                       #if both  elements of X and Y have the same index ($2+NR in the source file)
        dx=X[x]+0               #Store x value and add 0 to convert it to integer
        dy=Y[y]+0               #Store y value and add 0 to convert it to integer
        if ( dx >= max_x )     
           max_x=dx             #Is this the maximum x value ?
        if ( dy >= max_y )
           max_y=dy             #Is this the maximum y value ?
        gsub(/[0-9]+$/,"",x)      #get rid of the NR part
        f[dx,dy]=x              #Store the result in a 2D array
        break                   #Cause each element is unique ,dont need to look for another "x==y" so break second loop
        }                       #END if
                                #END Second loop
                                #END First loop
 
# SHOW RESULTS (i ll leave the presentation code for you)        
        
for (y=max_y;y>=1;y--)          #BEGIN coordinate y loop
    {
    printf("%.2d",y)
    for (x=1;x<=max_x;x++)      #BEGIN coordinate x loop
       {
       if ( f[x,y] == "" )      #Coordinates not displayed (null value in array)
           printf("  -")
       else
          printf(" %2s",f[x,y])
       }                        #END coordinate x loop --> Just print in the correct place the item. 
    printf("\n")
    }                           #END coordinate y loop         
printf("   ")
for (x=1;x<=max_x;x++)
   printf("%.2d ",x)
printf("\n")
} ' file
10  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  U  -  -  -  -
09  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
08  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
07  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
06  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
05  -  -  -  -  -  -  -  -  -  -  -  -  - GH  -  -  -  -  -  N
04  D HG  -  -  -  -  -  -  -  -  Ñ  -  -  -  F  -  -  -  -  -
03  C  G  K  -  -  -  -  -  U  -  -  -  -  -  -  -  -  -  -  -
02 BH  F LZ  -  -  -  -  -  - NX  -  -  -  -  -  -  -  - LL  -
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

# 11  
Old 02-28-2008
A final optimization , i notice that associative arrays ar useless here:
Code:
>awk '{
x=substr($1,1,2)+0
y=substr($1,3)+0
if ( x >= max_x )     
    max_x=x             #Is this the maximum x value ?
if ( y >= max_y )
   max_y=y             #Is this the maximum y value ?
f[x,y]=$2
}
END {
# SHOW RESULTS (i ll leave the presentation code for you)        
        
for (y=max_y;y>=1;y--)          #BEGIN coordinate y loop
    {
    printf("%.2d",y)
    for (x=1;x<=max_x;x++)      #BEGIN coordinate x loop
       {
       if ( f[x,y] == "" )      #Coordinates not displayed (null value in array)
           printf("  -")
       else
          printf(" %2s",f[x,y])
       }                        #END coordinate x loop --> Just print in the correct place the item. 
    printf("\n")
    }                           #END coordinate y loop         
printf("   ")
for (x=1;x<=max_x;x++)
   printf("%.2d ",x)
printf("\n")
} ' file

Code:
10  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  U  -  -  -  -
09  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
08  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
07  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
06  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
05  -  -  -  -  -  -  -  -  -  -  -  -  - GH  -  -  -  -  -  N
04  D HG  -  -  -  -  -  -  -  - Ñ  -  -  -  F  -  -  -  -  -
03  C  G  K  -  -  -  -  -  U  -  -  -  -  -  -  -  -  -  -  -
02 BH  F LZ  -  -  -  -  -  - NX  -  -  -  -  -  -  -  - LL  -
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

# 12  
Old 02-28-2008
Hi Klashxx,

Thnks !!! It really looks fabulous now .
I think it works now and codes are more concise after the optimisation.

Jus a question, what's the purpose of adding the " +0 " at the end of the codes. Is x and y arrays now ?

x=substr($1,1,2)+0
y=substr($1,3)+0


What if no " +0 " is added ? What will happen ?

Last edited by Raynon; 02-28-2008 at 10:57 AM..
# 13  
Old 02-28-2008
Quote:
Originally Posted by Raynon
Jus a question, what's the purpose of adding the " +0 " at the end of the codes. Is x and y arrays now ?

x=substr($1,1,2)+0
y=substr($1,3)+0


What if no " +0 " is added ? What will happen ?
You need it to force the conversion of the string that results of the substr function to a integer.
From The AWK Manual - Data Type Summary:

"The awk language defines comparisons as being done numerically if both operands are numeric, or if one is numeric and the other is a numeric string. Otherwise one or both operands are converted to strings and a string comparison is performed"

Remove +0 and look what happens.
# 14  
Old 03-01-2008
Hi Klashxx,

Thanks a million for for the guidance!!
I have definitely acquire alot of knowledge from you.SmilieSmilie
Smilie
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