BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"} 
Author Message
 BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

Bummer, is there no way to do
BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}
or
BEGIN{want[]={"s1o" "s2o" "s2q" "s3q"}
etc.?  It wasn't mentioned on:
File: gawk.info,  Node: Assigning Elements

Also, in the shell I can do "for i in s1o s2o s2q s3q; do"
In awk must I do
want[1]="slo"
want[2]="s2o"
want[3]="s2q"
want[4]="s3q"
for(i in want)
to get the same effect?  Gee, that's a lot more typing.
--
http://www.*-*-*.com/ +886-4-25854780 e-mail:restore .com.



Wed, 31 Dec 2003 09:35:24 GMT  
 BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

Quote:

> Bummer, is there no way to do
> BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}
> or
> BEGIN{want[]={"s1o" "s2o" "s2q" "s3q"}
> etc.?

Yes. There is no way to do this in awk, as there is in Perl:


Quote:
> Also, in the shell I can do "for i in s1o s2o s2q s3q; do"
> In awk must I do
> want[1]="slo"
> want[2]="s2o"
> want[3]="s2q"
> want[4]="s3q"
> for(i in want)
> to get the same effect?  Gee, that's a lot more typing.

And, more importantly, the order in which the elements are returned
is undefined for the "var in arr" construct. They generally will
*not* come out in insertion order.

Here's the awk idiom you're after, I think:

    BEGIN {
        n = split("slo s2o s2q s3q", want, " ")
        for (i = 1; i <= n; i++) {
            # do something with each of the elements want[1]...want[n]
        }
    }

Good luck!

--
Jim Monty

Tempe, Arizona USA



Wed, 31 Dec 2003 12:55:20 GMT  
 BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}
Hello,

...

Quote:
>> BEGIN{want[]={"s1o" "s2o" "s2q" "s3q"}
...
>  n = split("slo s2o s2q s3q", want, " ")

Looks pretty similar, isn't it? :-)
If you want to have a sorted field (the elements, indexes are droped) you can use
the new GAWK v3.1 feature "asort(field)".

  m = split("aa bb cc dd zz hh kk", feld)
  #asort(feld)
  for (i = 1; i <= m; i++)
    print i, feld[i]

When you enable the asort line, the elements will be, well, sorted. :-)

MfG, Dave

--
__________________________________________________________
News suchen, lesen, schreiben mit http://newsgroups.web.de



Wed, 31 Dec 2003 16:53:01 GMT  
 BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}


Quote:
>Bummer, is there no way to do
>BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}
>or
>BEGIN{want[]={"s1o" "s2o" "s2q" "s3q"}
>etc.?  It wasn't mentioned on:
>File: gawk.info,  Node: Assigning Elements

>Also, in the shell I can do "for i in s1o s2o s2q s3q; do"
>In awk must I do
>want[1]="slo"
>want[2]="s2o"
>want[3]="s2q"
>want[4]="s3q"
>for(i in want)
>to get the same effect?  Gee, that's a lot more typing.

Is this any use? It might be good enough if you only want to loop over a
list of values and don't need to store the values in an array.

BEGIN {
  $0="s1o s2o s2q s3q"
  for (i=1; i<=NF; i++) print $i # or do something else with $i

Quote:
}

--
Alan Linton


Wed, 31 Dec 2003 21:35:08 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

2. Parsing ""D""?

3. "Fifth", "Forth", zai nar?

4. Ruby "finalize", "__del__"

5. beginners "let"/"random" question

6. ANNOUNCE: new "plus"- and "dash"-patches available for Tcl7.5a2/Tk4.1a2

7. Looking for "stdin", "stdout"

8. Match "ab" in "abc", but not in "abd"

9. replace string AFTER "size","initial", "next"

10. Hi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "Two") text1.sub!("@BLAH", "Two") print text0,"\n" print text1,"\n" produces thiHi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "T

11. Lack of "D Tests", "E Tests", and "L Tests" for Generics

12. "?:", "a and b or c" or "iif"

 

 
Powered by phpBB® Forum Software