For Programmers: Free Programming Magazines  


Home > Archive > AWK > June 2005 > emulating sort -f









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 emulating sort -f
Robert Katz

2005-06-03, 8:55 pm

Let's say I have an array such as follows:

A[1] = "Space"
A[2] = "needle"

If I want to sort the array, but fold upper case into lower case,
I could do something like this:

for (a in A) {
AA[++k] = tolower(A[a])
Z[AA[k]] = A[a]
}
asort(AA)
for (i = 0; i < k;) print Z[AA[++i]]

but that depends on the tolower function being one to one over the array
elements. That is, if my array is

A[1] = "Space"
A[2] = "needle"
A[3] = "SPace"

then the above method will not work. Any ideas?

--
Regards,

---Robert
Bill Seivert

2005-06-04, 3:56 am



Robert Katz wrote:
> Let's say I have an array such as follows:
>
> A[1] = "Space"
> A[2] = "needle"
>
> If I want to sort the array, but fold upper case into lower case, I
> could do something like this:
>
> for (a in A) {
> AA[++k] = tolower(A[a])
> Z[AA[k]] = A[a]
> }
> asort(AA)
> for (i = 0; i < k;) print Z[AA[++i]]
>
> but that depends on the tolower function being one to one over the array
> elements. That is, if my array is
>
> A[1] = "Space"
> A[2] = "needle"
> A[3] = "SPace"
>
> then the above method will not work. Any ideas?
>


I would probably do something like:

for (a in A) {
B[a] = tolower(A[a]) " " A[a];
}
asort(B);

This will then have one record in B for every record in A.
Then for output, traverse B, and remove the first field.

Bill Seivert

Patrick TJ McPhee

2005-06-05, 3:55 am

In article <K55oe.6478$s04.1458@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:

[...]

% but that depends on the tolower function being one to one over the array
% elements. That is, if my array is
%
% A[1] = "Space"
% A[2] = "needle"
% A[3] = "SPace"
%
% then the above method will not work. Any ideas?

% for (a in A) {
AA[++k] = tolower(A[a]) SUBSEP a
% }
% asort(AA)
# note that your solution had these limits wrong
for (i = 1; i <= k; i++) {
split(AA[i], Z, SUBSEP)
print A[Z[2]]
}


--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Robert Katz

2005-06-06, 8:55 am

Robert Katz wrote:
> Let's say I have an array such as follows:
>
> A[1] = "Space"
> A[2] = "needle"
>
> If I want to sort the array, but fold upper case into lower case, I
> could do something like this:
>
> for (a in A) {
> AA[++k] = tolower(A[a])
> Z[AA[k]] = A[a]
> }
> asort(AA)
> for (i = 0; i < k;) print Z[AA[++i]]
>
> but that depends on the tolower function being one to one over the array
> elements. That is, if my array is
>
> A[1] = "Space"
> A[2] = "needle"
> A[3] = "SPace"
>
> then the above method will not work. Any ideas?
>

This seems to do what I want. Thanks for the suggestions.

A[1] = "Space"
A[2] = "needle"
A[3] = "SPace"
for (a in A) AA[++k] = tolower(A[a]) SUBSEP A[a]
asort(AA)
for (i = 0; i < k;) print Z[split(AA[++i], Z, SUBSEP)]

--
Regards,

---Robert
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com