Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message
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
Post Follow-up to this messageIn 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
Post Follow-up to this messageRobert 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.