Sorting arrays and calling system 
Author Message
 Sorting arrays and calling system

Hi all,

although I already found a workaround by using a shell-script calling
awk and sort one after the other, how do you experts sort arrays
(alphabetically)?
What is the mistake by expecting to have a shell-script started from
within an awk-script by coding retcode=system(sh nameofscript)?

Have a nice weekend everyone



Sun, 04 Nov 2001 03:00:00 GMT  
 Sorting arrays and calling system


Quote:
>although I already found a workaround by using a shell-script calling
>awk and sort one after the other, how do you experts sort arrays
>(alphabetically)?
>What is the mistake by expecting to have a shell-script started from
>within an awk-script by coding retcode=system(sh nameofscript)?

Many awks sort automatically when you use the 'for (i in array)' syntax
TAWK and probably a few others allow you to switch this
facility on/off....
Some members of the news group were irked by this!

What awks do you have?

Rgds
Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.efiche.com **



Sun, 04 Nov 2001 03:00:00 GMT  
 Sorting arrays and calling system

% although I already found a workaround by using a shell-script calling
% awk and sort one after the other, how do you experts sort arrays
% (alphabetically)?

Well, it depends what you want sorted, but to get printed output sorted,
you can pipe to sort from within awk:
  END { for (doctors in love) print love[doctors] | "sort"
        close("sort")
  }

or

  END { for (doctors in love) print doctors | "sort"
        close("sort")
  }

It's not so easy to get the output of sort back into awk (you generally
have to stick it in a temp file. If you're on a Unix system and don't
mind the thread of deadlock, you could make this temp file a FIFO which
might speed performance a bit.).

% What is the mistake by expecting to have a shell-script started from
% within an awk-script by coding retcode=system(sh nameofscript)?

I guess it depends on what you expect it to do. If you expect it to
put anything other than the return code set by an exit statement in
the script into retcode, that would be a mistake. If you expect
it to search a path for the script, that would be a mistake. If sh
and nameofscript aren't variables holding, respectively, "sh " and
the name of the script, that would be a huge mistake. Perhaps what
you want is
 retcode = system("sh nameofscript")
--

Patrick TJ McPhee
East York  Canada



Mon, 05 Nov 2001 03:00:00 GMT  
 Sorting arrays and calling system

This is was I used in gawk:

  # Sort an array indexed by number 0-(elements-1)
  function sortarrn(arrtosort,  elements) {
    for (x=0; x<elements; x++)
      for (y=x+1; y<elements; y++)
        if (arrtosort[y] < arrtosort[x]) {
          t = arrtosort[x]
          arrtosort[x] = arrtosort[y]
          arrtosort[y] = t
        }
  }

  # Copy the string-indexed array to a number-indexed
  # array and call sort function.
  bln=0
  for (block in blocklist) {
    blockarr[bln++] = block
  }
  sortarrn(blockarr, bln)

  # blockarr[] now contains a sorted list with index 0-bln.

A bit overkill maybe. Anyone having a better method to
sort an array indexed by a string directly without this
conversion first?

Bo Siltberg

Quote:

> Hi all,

> although I already found a workaround by using a shell-script calling
> awk and sort one after the other, how do you experts sort arrays
> (alphabetically)?
> What is the mistake by expecting to have a shell-script started from
> within an awk-script by coding retcode=system(sh nameofscript)?

> Have a nice weekend everyone



Mon, 05 Nov 2001 03:00:00 GMT  
 Sorting arrays and calling system
Hit this url:

http://plan9.bell-labs.com/netlib/research/awkbookcode/index.html

Chapters 6 and 7 have many sorting examples (qsort, isort, etc).  Maybe
it'll help.

-te

Quote:

> A bit overkill maybe. Anyone having a better method to
> sort an array indexed by a string directly without this
> conversion first?

--
Troy Engel
CME, ABT Corporation


Mon, 05 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. HELP: array and system call

2. Array#sort! returns nil when array empty

3. system calls - file system structure

4. IBM system 370 system call "DMXINV"

5. exploratory system for unix system calls/facilities

6. WANTED: a way to sort array so each column is in des

7. WANTED: APL-II way to sort array so each column is in descending order

8. Space efficient sorting of arrays

9. Space efficient sorting of arrays

10. 2-D arrays/sorted collections

11. J/APL memory use [was Re: SV: Space efficient sorting of arrays]

12. re efficient sorting of arrays

 

 
Powered by phpBB® Forum Software