For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2007 > access to index of source array after asort









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 access to index of source array after asort
Seb

2007-04-27, 9:56 pm

Hi,

I'd like to use a variable 'v' for an awk program, so that I can check the
entire lines after they've been sorted and parsed. Suppose
we have these data:


one a 69.37 226.38 58
two b 69.5 226.42 195
three c 69.35 235.94 36


and need to sort lines according to field 4 (descending) and add other
information:


{
arr[$0]=sprintf("%.4f %.4f", $4, $3)
}

END {
arr_sort=asort(arr)
for (i = arr_sort; i >= 1; i--) {
print coords[i]
}
}


If the 'v' variable is set (its value doesn't matter), I'd like to send
the full lines (which have been stored as the index of arr above) to
/dev/stderr, sorted as in arr_sort. I thought of using another array, but
it seems wasteful in this case. It seems better to access the indices of
the source array, but how can they be sorted this way? Thanks.



Cheers,

--
Seb
Seb

2007-04-28, 7:56 am

On Sat, 28 Apr 2007 01:12:54 GMT,
Seb <spluque@gmail.com> wrote:

[...]

> If the 'v' variable is set (its value doesn't matter), I'd like to send
> the full lines (which have been stored as the index of arr above) to
> /dev/stderr, sorted as in arr_sort. I thought of using another array,
> but it seems wasteful in this case. It seems better to access the
> indices of the source array, but how can they be sorted this way?
> Thanks.


I got something working, but it feels contrived:


---<---------------cut here---------------start-------------->---
{
ll=sprintf("%.4f %.4f", $4, $3)
full=sprintf("%s %s %s %s", ll, $1, $2, $5)
arr[full]=ll
}

END {
n=asort(arr, arr_s)
for (i = n; i >= 1; i--) {
print arr_s[i]
}
if (v) {
j=1
for (i in arr) {b[j]=i; j++}
n=asort(b)
for (i=n; i >= 1; i--) {print b[i] > "/dev/stderr"}
}
}
---<---------------cut here---------------end---------------->---


Apologies for the nonsense coords array in my previous post (should have
read arr).


--
Seb
Ed Morton

2007-04-30, 6:57 pm

Seb wrote:
> On Sat, 28 Apr 2007 01:12:54 GMT,
> Seb <spluque@gmail.com> wrote:
>
> [...]
>
>
>
>
> I got something working, but it feels contrived:
>
>
> ---<---------------cut here---------------start-------------->---
> {
> ll=sprintf("%.4f %.4f", $4, $3)
> full=sprintf("%s %s %s %s", ll, $1, $2, $5)
> arr[full]=ll
> }
>
> END {
> n=asort(arr, arr_s)
> for (i = n; i >= 1; i--) {
> print arr_s[i]
> }
> if (v) {
> j=1
> for (i in arr) {b[j]=i; j++}
> n=asort(b)
> for (i=n; i >= 1; i--) {print b[i] > "/dev/stderr"}
> }
> }
> ---<---------------cut here---------------end---------------->---
>
>
> Apologies for the nonsense coords array in my previous post (should have
> read arr).
>
>


Why not just store the full lines but only print out the first 2 when
you want, e.g.:

---<---------------cut here---------------start-------------->---
{
full=sprintf("%.4f %.4f %s %s %s", $4, $3, $1, $2, $5)
arr[full]=full
}

END {
n=asort(arr)
for (i = n; i >= 1; i--) {
split(arr[i],arr_ll)
print arr_ll[1],arr_ll[2]
}
if (v) {
for (i=n; i >= 1; i--) {print arr[i] > "/dev/stderr"}
}
}
---<---------------cut here---------------end---------------->---

Note that using "full" as both the index and the contents for arr will
remove duplicates which is apparently what you want. If you want to
preserve duplicates, just use some self-incrementing variable as the index.

Regards,

Ed.

Sponsored Links







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

Copyright 2008 codecomments.com