The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array with special Characters donaldfung UNIX for Dummies Questions & Answers 1 06-08-2008 09:18 AM
How to declare an array to take more than 10,000 characters pinky Shell Programming and Scripting 0 01-15-2008 06:38 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 05:42 AM
Display special characters BCarlson Shell Programming and Scripting 2 10-06-2006 06:59 AM
Display EBCDIC as Characters LouPelagalli AIX 1 08-09-2005 11:07 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-27-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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 03:26 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-27-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 371
Stumble this Post!
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
Reply With Quote
  #3 (permalink)  
Old 02-27-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 02-27-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 371
Stumble this Post!
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 06:25 AM. Reason: bug solved
Reply With Quote
  #5 (permalink)  
Old 02-27-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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
Reply With Quote
  #6 (permalink)  
Old 02-27-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 371
Stumble this Post!
Hi , change last print for printf("\n")
Reply With Quote
  #7 (permalink)  
Old 02-27-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0