![]() |
|
|
|
|
|||||||
| 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 | Search this Thread | Display Modes |
|
#8
|
||||
|
||||
|
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 |
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 07:57 AM. |
|
#13
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
Hi Klashxx,
Thanks a million for for the guidance!! I have definitely acquire alot of knowledge from you. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|