How can i do Recursive function in AWK? 
Author Message
 How can i do Recursive function in AWK?

How can i do Awk function recursive???

Example:

awk'

funtion xx (p1,p2){

Quote:
}

{
xx (p1,p2)
Quote:
}



Mon, 16 Oct 2000 03:00:00 GMT  
 How can i do Recursive function in AWK?

: How can i do Awk function recursive???

: Example:

: awk'

: funtion xx (p1,p2){
: }

: {
: xx (p1,p2)
: }

I presume you mean something of the order this utility which recursively
descends a directory and reports the size of the characters of
all the files in the directory.

#!/usr/local/bin/gawk -f
function help(helpmsg) {
   fmt = sprintf("Format: %s", ARGV[0])
   printf "%s [o=(h[elp]|d[ebug]|r)] dirname\n",fmt
   if (length(helpmsg)) print helpmsg
   printf "%*s help       this message\n",length(fmt)," "
   printf "%*s debug      print diagnostic messages\n",length(fmt)," "
   printf "%*s o=help     this message\n",length(fmt)," "
   printf "%*s o=debug    print debug messages\n",length(fmt)," "
   printf "%*s o=r        follow subdirs recursively\n",length(fmt)," "
   printf "%*s dirname    examine directory dirname [default .]\n",length(fmt)," "
   exit_immediately = 1
   exit
   }

function do_dir(directory,  dname, dirct, i) { #dname, dirct, i are automatic
   ls = "cd " directory ";ll -i"
   while ((ls | getline) > 0) {
      if ($2 ~ /^-/) {
         SZ[$1] = $6
         }
      else if ($2 ~ /^d/) {
         SZ[$1] = $6
         if (recursive) {
            dname[++dirct] = $NF
            }
         }
      }
   close(ls)
   for (i=1;i<=dirct;i++) {
      do_dir(directory "/" dname[i])
      }
   }

function int2comma(value,  iarr, ict, cval, i) {
   integer = value
   do {
      iarr[++ict] = integer%1000
      integer = int(integer/1000)
      }
   while (integer)
   for (i=ict;i;i--) {
      cval = sprintf("%s%.3d%s",cval,iarr[i],(i>1) ? "," : "")
      }
   sub(/^0*/,//,cval)
   return(cval)
   }

BEGIN {
for (i=1;i<ARGC;i++) {
   if (ARGV[i] ~ /^o=/) {
      # check options
      oct = split(ARGV[i],option,/[=,]/)
      for (j=2;j<=oct;j++) {
         if ("help" ~ option[j]) help("")
         else if ("debug" ~ option[j]) debug_flag=1
         else if ("r" == option[j]) recursive=1
         else
            help("Invalid option " option[j])
         }
      }
   else {
      # a directory name
      if (dirname) help("Only one dirname allowed")
      dirname = ARGV[i]
      if (system("cd " dirname)) {
         help("Can't change to directory " dirname)
         }
      }
   }
dirname = (dirname) ? dirname : "."
pwd = "cd " dirname ";sh -c pwd"
pwd|getline path
do_dir(dirname)
for (i in SZ) size += SZ[i]
printf "%s contains %s total bytes\n", path, int2comma(size)

Quote:
}

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Mon, 16 Oct 2000 03:00:00 GMT  
 How can i do Recursive function in AWK?

Quote:

>How can i do Awk function recursive???

Er, the same as any other language with functions.

Quote:
>Example:

>funtion xx (p1,p2){
>}

>{
>xx (p1,p2)
>}

This isn't recursive.  This is an example of a recursive function:

        function fibonacci(n) {

                return (n > 1 ? fibonacci(n-1) + fibonacci(n-2) : n)
        }

Ralph
--



Tue, 17 Oct 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Recursive function defined within function => NameError

2. Pickler that handles modules, functions, and builtin functions -- done

3. Neat recursive menu program written in awk

4. Can this be done in AWK?

5. How is this done in awk/nawk/gawk?

6. Can this be done with AWK or ED ?

7. Recursive once functions in the Parse library classes?

8. Mutually recursive functions in FORTH

9. recursive functions

10. Recursive functions

11. Typed Lambda Calculi and Total Recursive Functions

12. Logo SEGS package for generating recursive curve functions

 

 
Powered by phpBB® Forum Software