![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 07: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 04:26 AM. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
Last edited by Klashxx; 02-27-2008 at 07:25 AM. Reason: bug solved |
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Hi , change last print for printf("\n")
|
|
#7
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|