Home > Archive > AWK > February 2005 > 3d arrays - sorting!?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
3d arrays - sorting!?
|
|
| Gernot Frisch 2005-02-16, 3:56 pm |
| oh dear:
assume an array of balls. Each ball has a number, a colur and a taste
and inside the ball there's a value.
data[nr, color, taste] = value;
now write a list that is sorted this way:
nr=1
color=red
taste=chery
taste=berry
color=blue
taste=plum
nr=2
color=yellow
taste=banana
....
thus, sort via nr->color->taste for each element...
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
| |
| Jürgen Kahrs 2005-02-16, 3:56 pm |
| Gernot Frisch wrote:
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
> ...
>
> thus, sort via nr->color->taste for each element...
Problems like these are usually solved with
a combination of Bourne shell sorting and
AWK printing.
First AWK prints all the lines with each dimension
in a column and the data in an extrax column. This
is piped into "sort" and "sort" uses the extra column
as a sort criterium. The result can be piped back
into AWK, if necessary.
| |
| Ed Morton 2005-02-16, 3:56 pm |
|
Gernot Frisch wrote:
> oh dear:
>
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
>
> now write a list that is sorted this way:
>
> nr=1
> color=red
> taste=chery
> taste=berry
> color=blue
> taste=plum
> nr=2
> color=yellow
> taste=banana
>
> ...
>
> thus, sort via nr->color->taste for each element...
For the "nr", the order is probably numerical, but what about the rest?
Why does "red" come before "blue" for color (reverse alphabetical?
wavelength?) and "chery" before "berry" in your sample output? Do you
actually not care about sorting, but more about grouping the colors and
tastes? Do you actually care about the order of the nrs or would it be
OK if they came out as 7, 2, 15, ... or some other apparently random
order as long as the colors are grouped within each number and tastes
grouped within each color per nr?
Ed.
| |
| William James 2005-02-17, 3:57 am |
| Gernot Frisch wrote:
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
>
> now write a list that is sorted this way:
>
> nr=1
> color=red
> taste=chery
> taste=berry
> color=blue
> taste=plum
> nr=2
> color=yellow
> taste=banana
>
> ...
>
> thus, sort via nr->color->taste for each element...
The following code has been tested only briefly;
use at your own risk.
*BEGIN {
* data[2,"red","cherry"] = 13
* data[11,"green","kiwi"] = 111
* data[2,"blue","plum"] = 8
* data[2,"blue","grape"] = 77
* data[2,"blue","blueberry"] = 56
* data[11,"yellow","banana"] = 114
*
* sort( data, sorted )
* for (i=1; i in sorted; i++)
* { split( sorted[i], tmp, SUBSEP )
* printf "%5d %-9s%-12s%s\n",
* tmp[1],tmp[2],tmp[3],tmp[4]
* }
*}
*
*function sort( A, A_out ,key,entry,j,k )
*{ split( "", A_out )
* for ( key in A )
* { entry = key SUBSEP A[key]
* for (j=1;j<=length(A_out);j++)
* if ( less(entry,A_out[j]) )
* break
* for (k=length(A_out)+1;k>j;k--)
* A_out[k] = A_out[k-1]
* A_out[k] = entry
* }
*}
*
*function less( s1, s2 ,t1,t2,i,a,b)
*{ split( s1, t1, SUBSEP )
* split( s2, t2, SUBSEP )
* for (i=1; i<length(t1); i++)
* { a = t1[i]
* b = t2[i]
* if (a == b)
* continue
* return (a < b)
* }
* return 0
*}
The output is
2 blue blueberry 56
2 blue grape 77
2 blue plum 8
2 red cherry 13
11 green kiwi 111
11 yellow banana 114
| |
|
| Oh boy ...TAWK can do this easily...
Now TAWK uses multidimensional arrays, and one can use automatic sorting
with the for (i in data) construct. Sorting can be made to go hi-to-low by
setting SORTTYPE = 9. When I was introduced to AWK years ago, I was told to
get TAWK. I always assumed that what TAWK could do, AWK could too, only
compiled. I'm still suffering through the unique AWK logic such as
automatic loops and Regex. But when I go to other languages I'm really lost
and pissed off I can't do the things that are simple in TAWK. Now TAWK is no
longer supported. Does anyone know if Thompson Automation will reliquish
some its rights so that TAWK can be shared with others? I would really like
it if smarter programmers than me were pounding away with TAWK... I would
love to get pointers. And finally will some of
TAWK's features be considered for GAWK, i.e. pack, unpack and of course
multi-D arrays.
INIT{
data[1]["red"]["berry"] = "1 red berry"
data[1]["red"]["chery"] = "1 red chery"
data[2]["yellow"]["banana"] = "2 yellow banana"
data[1]["blue"]["plum"] = "1 blue plum"
}
BEGIN{
SORTTYPE = 1
for (i in data) {
SORTTYPE = 9
for (j in data[i]) {
SORTTYPE = 9
for (k in data[i][j]) {
print data[i][j][k]
}
}
}
}
1 red chery
1 red berry
1 blue plum
2 yellow banana
REX
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:37h2qkF5eimutU1@individual.net...
> oh dear:
>
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
>
> now write a list that is sorted this way:
>
> nr=1
> color=red
> taste=chery
> taste=berry
> color=blue
> taste=plum
> nr=2
> color=yellow
> taste=banana
>
> ...
>
> thus, sort via nr->color->taste for each element...
>
>
>
> --
> -Gernot
> int main(int argc, char** argv) {printf
> ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
>
> ________________________________________
> Looking for a good game? Do it yourself!
> GLBasic - you can do
> www.GLBasic.com
>
>
| |
| Kenny McCormack 2005-02-17, 3:57 am |
| In article <B4GdnYamiYNHPY7fRVn-vA@comcast.com>, trexx <foo@foo.com> wrote:
>Oh boy ...TAWK can do this easily...
>Now TAWK uses multidimensional arrays, and one can use automatic sorting
>with the for (i in data) construct. Sorting can be made to go hi-to-low by
>setting SORTTYPE = 9. When I was introduced to AWK years ago, I was told to
>get TAWK. I always assumed that what TAWK could do, AWK could too, only
>compiled. I'm still suffering through the unique AWK logic such as
>automatic loops and Regex. But when I go to other languages I'm really lost
>and pissed off I can't do the things that are simple in TAWK. Now TAWK is no
>longer supported. Does anyone know if Thompson Automation will reliquish
>some its rights so that TAWK can be shared with others? I would really like
>it if smarter programmers than me were pounding away with TAWK... I would
>love to get pointers.
Somebody should put out an APB on Pat Thompson.
At this point in time, TAWK is basically supported by... (drum roll)
Me! I know the language pretty darn well (think "Fargo") and I can
probably answer any questions you might have.
What OS are you using TAWK with?
>And finally will some of TAWK's features be considered for GAWK, i.e.
>pack, unpack and of course multi-D arrays.
I doubt the maintainers would ever go along with implementing true multi-D
arrays - nice as it would be. It's a lot of work. My guess is that TAWK
designed it in from the beginning and it's just too late in the game for
most existing AWKs. But then again, I still don't know where "switch" came
from - why they decided to add that to GAWK, so, ..., stranger things have
happened.
I've actually thought about implementing pack/unpack (as a shared lib
extension) for GAWK. It would go along well with my call_any() extension,
which gives GAWK almost as much power/ease to call OS routines as TAWK has.
| |
| Ed Morton 2005-02-17, 3:57 am |
|
Ed Morton wrote:
>
>
> Gernot Frisch wrote:
>
>
>
> For the "nr", the order is probably numerical, but what about the rest?
> Why does "red" come before "blue" for color (reverse alphabetical?
> wavelength?) and "chery" before "berry" in your sample output? Do you
> actually not care about sorting, but more about grouping the colors and
> tastes? Do you actually care about the order of the nrs or would it be
> OK if they came out as 7, 2, 15, ... or some other apparently random
> order as long as the colors are grouped within each number and tastes
> grouped within each color per nr?
>
> Ed.
Assuming you're just trying to group the information, this is all you
need to do:
awk '{
# create 3 arrays - one to hold all the numbers,
# one to hold all the colors for each number, and
# one to hold all the fruits for each color/number combination
split($1,a,/[],[]/)
nrsA[a[2]]=""
colorsA[a[2]] = colorsA[a[2]] "," a[3]
fruitsA[a[2],a[3]] = fruitsA[a[2],a[3]] "," a[4]
}
END{
for (nr in nrsA) {
print "nr=" nr
# get all the colors for this number
c = split(colorsA[nr],tmpA,",")
for (i=2;i<=c;i++) { colors[tmpA[i]]="" }
delete tmpA
for (color in colors) {
print "\tcolor=" color
# get all the fruits for this color in this number
c = split(fruitsA[nr,color],tmpA,",")
for (i=2;i<=c;i++) { fruits[tmpA[i]]="" }
delete tmpA
for (fruit in fruits) {
print "\t\tfruit=" fruit
}
delete fruits
}
delete colors
}
}'
If you use gawk and you have some specific sort order in mond, you can
sort the output order by using asorti() and/or asort() and/or setting
the WHINY_USERS flag before exeuction.
Regards,
Ed.
| |
|
| Kenny
Good to hear of your expertise... I'll be bugging you for sure in the
future! I now almost exclusively use Win2K. However believe it or not, I
still use the Dos version on computers that are running legacy code from
another millenium. When I was travelling more as a mining consultant, I put
the Dos version of Tawk on my HP 200LX pocket pc! For years I fiddled with
Pat's excellent Tawk manual...it's really dog eared now!... and tried awk
commands while on plane, train, hotel rooms etc. etc. Programming doesn't
come easily for me and awk is/ has been a steep climb. But it's worth
it...and I always seem to come back to awk and Tawk to solve REAL problems
.... including getting the JOB done while the young programmers are still
talking about object oriented this, OLE that, COM, C#, parallel processes
etc. etc. etc. Jeeze, if I could only BS as well. With Tawk, persperation
and only a little inspiration I've been able to get the something
accomplished... while the youngsters are still talking about how great their
solution will be with GUI interfaces.... By the way is Tawk on Linux? ... I
bought myself Red Hat as a Christmas present and the first thing I did was
to write an awk program that counted to a zillion... very satisfying in an
odd way...
REX
"Kenny McCormack" <gazelle@yin.interaccess.com> wrote in message
news:cv08tg$erl$1@yin.interaccess.com...
> In article <B4GdnYamiYNHPY7fRVn-vA@comcast.com>, trexx <foo@foo.com>
wrote:
by[color=darkred]
to[color=darkred]
lost[color=darkred]
no[color=darkred]
like[color=darkred]
>
> Somebody should put out an APB on Pat Thompson.
>
> At this point in time, TAWK is basically supported by... (drum roll)
> Me! I know the language pretty darn well (think "Fargo") and I can
> probably answer any questions you might have.
>
> What OS are you using TAWK with?
>
>
> I doubt the maintainers would ever go along with implementing true multi-D
> arrays - nice as it would be. It's a lot of work. My guess is that TAWK
> designed it in from the beginning and it's just too late in the game for
> most existing AWKs. But then again, I still don't know where "switch"
came
> from - why they decided to add that to GAWK, so, ..., stranger things have
> happened.
>
> I've actually thought about implementing pack/unpack (as a shared lib
> extension) for GAWK. It would go along well with my call_any() extension,
> which gives GAWK almost as much power/ease to call OS routines as TAWK
has.
>
| |
| Jürgen Kahrs 2005-02-20, 8:55 pm |
| Gernot Frisch wrote:
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
> ...
>
> thus, sort via nr->color->taste for each element...
Problems like these are usually solved with
a combination of Bourne shell sorting and
AWK printing.
First AWK prints all the lines with each dimension
in a column and the data in an extrax column. This
is piped into "sort" and "sort" uses the extra column
as a sort criterium. The result can be piped back
into AWK, if necessary.
| |
| Ed Morton 2005-02-20, 8:55 pm |
|
Gernot Frisch wrote:
> oh dear:
>
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
>
> now write a list that is sorted this way:
>
> nr=1
> color=red
> taste=chery
> taste=berry
> color=blue
> taste=plum
> nr=2
> color=yellow
> taste=banana
>
> ...
>
> thus, sort via nr->color->taste for each element...
For the "nr", the order is probably numerical, but what about the rest?
Why does "red" come before "blue" for color (reverse alphabetical?
wavelength?) and "chery" before "berry" in your sample output? Do you
actually not care about sorting, but more about grouping the colors and
tastes? Do you actually care about the order of the nrs or would it be
OK if they came out as 7, 2, 15, ... or some other apparently random
order as long as the colors are grouped within each number and tastes
grouped within each color per nr?
Ed.
| |
|
| Oh boy ...TAWK can do this easily...
Now TAWK uses multidimensional arrays, and one can use automatic sorting
with the for (i in data) construct. Sorting can be made to go hi-to-low by
setting SORTTYPE = 9. When I was introduced to AWK years ago, I was told to
get TAWK. I always assumed that what TAWK could do, AWK could too, only
compiled. I'm still suffering through the unique AWK logic such as
automatic loops and Regex. But when I go to other languages I'm really lost
and pissed off I can't do the things that are simple in TAWK. Now TAWK is no
longer supported. Does anyone know if Thompson Automation will reliquish
some its rights so that TAWK can be shared with others? I would really like
it if smarter programmers than me were pounding away with TAWK... I would
love to get pointers. And finally will some of
TAWK's features be considered for GAWK, i.e. pack, unpack and of course
multi-D arrays.
INIT{
data[1]["red"]["berry"] = "1 red berry"
data[1]["red"]["chery"] = "1 red chery"
data[2]["yellow"]["banana"] = "2 yellow banana"
data[1]["blue"]["plum"] = "1 blue plum"
}
BEGIN{
SORTTYPE = 1
for (i in data) {
SORTTYPE = 9
for (j in data[i]) {
SORTTYPE = 9
for (k in data[i][j]) {
print data[i][j][k]
}
}
}
}
1 red chery
1 red berry
1 blue plum
2 yellow banana
REX
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:37h2qkF5eimutU1@individual.net...
> oh dear:
>
> assume an array of balls. Each ball has a number, a colur and a taste
> and inside the ball there's a value.
>
> data[nr, color, taste] = value;
>
> now write a list that is sorted this way:
>
> nr=1
> color=red
> taste=chery
> taste=berry
> color=blue
> taste=plum
> nr=2
> color=yellow
> taste=banana
>
> ...
>
> thus, sort via nr->color->taste for each element...
>
>
>
> --
> -Gernot
> int main(int argc, char** argv) {printf
> ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
>
> ________________________________________
> Looking for a good game? Do it yourself!
> GLBasic - you can do
> www.GLBasic.com
>
>
|
|
|
|
|