Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
06-04-05 01:55 AM


Re: emulating sort -f

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


Report this thread to moderator Post Follow-up to this message
Old Post
Bill Seivert
06-04-05 08:56 AM


Re: emulating sort -f
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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
06-05-05 08:55 AM


Re: emulating sort -f
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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
06-06-05 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:48 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.