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 and shell scripting languages 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 01:18 PM
How to declare an array to take more than 10,000 characters pinky Shell Programming and Scripting 0 01-15-2008 10:38 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 09:42 AM
Display special characters BCarlson Shell Programming and Scripting 2 10-06-2006 10:59 AM
Display EBCDIC as Characters LouPelagalli AIX 1 08-09-2005 03:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-27-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 02-27-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 393
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 (permalink)  
Old 02-27-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 02-27-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 393
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 (permalink)  
Old 02-27-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 02-27-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 393
Hi , change last print for printf("\n")
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0