how to do "case/switch" in awk 
Author Message
 how to do "case/switch" in awk

Hello awk professionals, I was wondering how to enjoy the ease of the
case/switch statement of the shell and C, in awk?  How would one write
it?  Would one merely use:

Aharon> if ... else if ...

Also, if as in C, unlike the shell, I wanted to only do the 'switch' amongst
integer choices, is there a better way [more smart looking or more CPU
efficient [or am I already as efficient as I can get anyway]]?  Would
it involve arrays?
--
http://www.*-*-*.com/ :restore .com.



Wed, 10 Dec 2003 13:05:06 GMT  
 how to do "case/switch" in awk

Quote:
> case/switch statement of the shell and C, in awk?  How would one write
> it?  Would one merely use:

> Aharon> if ... else if ...

In awk case is the outermost control structure. One used to explain it's inner workings using

while(getline()){
        switch (line){
                pattern0 { action0 }
                ...
                patternN { actionN }

Quote:
}

Heh, that's one of the awk gotchas.

The best way is (at least it was in many cases) to rethink problem to move
`switch'ing to natural /pattern/{action} schema.

In other cases you have (IMHO) to use `if's...

--

Written at:
 # . . .
 . # # .
 . # . .
 # # . .



Thu, 11 Dec 2003 22:25:48 GMT  
 how to do "case/switch" in awk

Quote:

> Hello awk professionals, I was wondering how to enjoy the ease of the
> case/switch statement of the shell and C, in awk?  How would one write
> it?  Would one merely use:

> Aharon> if ... else if ...

> Also, if as in C, unlike the shell, I wanted to only do the 'switch' amongst
> integer choices, is there a better way [more smart looking or more CPU
> efficient [or am I already as efficient as I can get anyway]]?  Would
> it involve arrays?

If you have a long "if ... else if ..." serpent to just map some values,
you can better use arrays. Instead of:

{
  if(1==foo)
    bar = 27
  else if(2==foo)
    bar = 53
  else if("baz"==foo)
    bar = "bla"
  ...

Quote:
}

With an array, you initialize it in the BEGIN section:

BEGIN \
{
  BAR[1] = 27
  BAR[2] = 53
  BAR["baz"] = "bla"
  ...

Quote:
}

... and can use it like this:

  bar = BAR[foo]

or if you might receive some unknown "foo" values:

  if(foo in BAR)
    bar = BAR[foo]
  else
    bar = "something else"

This looks a lot nicer than dozens of "if ... else if" lines (personal
opinion, of course). Whether it is more efficient depends on your
problem, your awk version, your machine, the phase of the moon... If you
really have to save some microseconds and really can't optimize other
parts of your approach, you will have to try it for yourself. In
general, you won't even notice the difference, and probably there are
other places that would benefit more from optimizing (don't spawn a new
process in every second line of a shell script, don't "cat|awk", don't
"grep|awk" and so on).

Regards...
                Michael



Tue, 16 Dec 2003 19:10:20 GMT  
 how to do "case/switch" in awk
I'm a Masters in Computer Science, but that was a long time ago, so
when I see the young wippersnappers writing

Michael>   if(1==foo) #hey whydidyousaythat?
Michael>     bar = 27
Michael>   else if(2==foo) #he did it again
Michael>     bar = 53
Michael>   else if("baz"==foo) #damn, he's serious

I wonder what secret efficiency do they gain over the old fud way
"if(foo==1)" etc.?

Also I don't understand
Rafal> Written at:
Rafal>  # . . .
Rafal>  . # # .
Rafal>  . # . .
Rafal>  # # . .
certainly it must be a secret time or location or both sequence.
Does it have something to do with the moon jazz?

Feeling old and out of the loop,
--
http://www.geocities.com/jidanni Tel+886-4-25854780 e-mail:restore .com.



Wed, 31 Dec 2003 10:47:35 GMT  
 how to do "case/switch" in awk

Quote:

> I'm a Masters in Computer Science, but that was a long time ago, so
> when I see the young wippersnappers writing

> Michael>   if(1==foo) #hey whydidyousaythat?
> Michael>     bar = 27
> Michael>   else if(2==foo) #he did it again
> Michael>     bar = 53
> Michael>   else if("baz"==foo) #damn, he's serious

> I wonder what secret efficiency do they gain over the old fud way
> "if(foo==1)" etc.?

Have you noticed their clothing and taste in music are different, too?

Relax. It's fashion, not function.

Quote:
> Feeling old and out of the loop,

We old codgers know that variables go on the left, constants on
the right. I'm not gonna wear "shorts" that extend to the middle
of my calves and could fit ten of my legs. I'm sure as hell not
gonna flip my comparison operands!

Their way suX, our way RooLz!

--
Jim Monty

Tempe, Arizona USA



Wed, 31 Dec 2003 14:50:12 GMT  
 how to do "case/switch" in awk
Hello,

Quote:

>I'm a Masters in Computer Science, but that was a long time
>ago, so when I see the young wippersnappers writing

>Michael>   if(1==foo) #hey whydidyousaythat?
>Michael>     bar = 27
>Michael>   else if(2==foo) #he did it again
>Michael>     bar = 53
>Michael>   else if("baz"==foo) #damn, he's serious

They think they can better avoid typos. If you want to say:
 if (var == 1)
but accidentally write:
 if (var = 1)
you got of course unwanted effects.

so if they write:
 if (1 == var)
they cannot accidentally type:
 if (1 = var)
because the compiler/interpreter sees that.

I used to do this, too. However I realized soon that you destroy the readability
and hence maintainability of your programs and in the end this hurts much more.
So I droped it.
I don't know though why the fathers of C, AWK etc didn't just use the
mathematical correct way like Pascal/Delphi did it: always use ":=" for variable
assignments and "=" for ifs. This saved so many typos. But that's another topic.
:-)

MfG, Dave

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



Wed, 31 Dec 2003 15:29:09 GMT  
 how to do "case/switch" in awk


% when I see the young wippersnappers writing
%
% Michael>   if(1==foo) #hey whydidyousaythat?

% I wonder what secret efficiency do they gain over the old fud way
% "if(foo==1)" etc.?

I don't know why Michael writes that way, but if you leave out
an =, `if (1 = foo)' will generate a syntax error, while
`if (foo = 1) ' won't.

--

Patrick TJ McPhee
East York  Canada



Wed, 31 Dec 2003 22:50:27 GMT  
 how to do "case/switch" in awk
...

Quote:
>They think they can better avoid typos. If you want to say:
> if (var == 1)
>but accidentally write:
> if (var = 1)
>you got of course unwanted effects.

>so if they write:
> if (1 == var)
>they cannot accidentally type:
> if (1 = var)
>because the compiler/interpreter sees that.

>I used to do this, too. However I realized soon that you destroy the
readability
>and hence maintainability of your programs and in the end this hurts much
more.
>So I droped it.

Speaking as a mathematician by training, when does  (x == 1) != (1 == x)?

As for readability, if you can read APL or Forth, nothing in C etc. seems
unreadable.

Quote:
>I don't know though why the fathers of C, AWK etc didn't just use the
>mathematical correct way like Pascal/Delphi did it: always use ":=" for
variable
>assignments and "=" for ifs. This saved so many typos. But that's another

topic.

As you point out above, (1 = x) will catch typos quick enough. For me,
Ritchie got it exactly right - assignment operations are far more frequent
than equality testing, so make assignment operations (marginally) faster &
easier. Besides, if one came from fortran the equality comparison operation
was .EQ. and assignment =. C just simplified the equality comparison
operator. Not that the Pascal way doesn't introduce some rather obscure bugs
itself (see Kernighan's classic essay 'Why Pascal Is Not My Favorite
Programming Language').



Thu, 01 Jan 2004 16:31:24 GMT  
 how to do "case/switch" in awk

Quote:

>As you point out above, (1 = x) will catch typos quick enough. For me,
>Ritchie got it exactly right - assignment operations are far more frequent
>than equality testing, so make assignment operations (marginally) faster &
>easier. Besides, if one came from FORTRAN the equality comparison operation
>was .EQ. and assignment =. C just simplified the equality comparison
>operator. Not that the Pascal way doesn't introduce some rather obscure bugs
>itself (see Kernighan's classic essay 'Why Pascal Is Not My Favorite
>Programming Language').

There's a mention of this in the History of C paper.  `=' as opposed to
':=' was used to simplify the lexical analyzer for B on the PDP-7; the
machine would constantly run out of core interpreting programs, so tiny
micro-optimizations were made to reduce the interpreter's size.  It's
usage in C is simply a hold over.  BCPL used `:='.  Incidentally, this
is also where `++' and `+=' come from (it's a common misconception that
the former was derived from instructions on the PDP-11 which
auto-incremented their operands, but the usage predates the PDP-11's
introduction into 1127).

        - Dan C.



Thu, 01 Jan 2004 22:56:41 GMT  
 how to do "case/switch" in awk
Hi!

...

Quote:
>Speaking as a mathematician by training, when
>does  (x == 1) != (1 == x)?

Not this one, but this one hurts IMHO: "x = x + 1" and all that like. :-))

Quote:
>As for readability, if you can read APL or Forth, nothing in
>C etc. seems unreadable.

I didn't mean C as such, but the "if (0 == var) .. " thingy. If you talk to
someone you usually will say "if your variable reaches 0 you're going to do this
and that", I think.

MfG, Dave

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



Fri, 02 Jan 2004 01:09:30 GMT  
 how to do "case/switch" in awk

Quote:


> ...
>>They think they can better avoid typos. If you want to say:
>> if (var == 1)
>>but accidentally write:
>> if (var = 1)
>>you got of course unwanted effects.

>>so if they write:
>> if (1 == var)
>>they cannot accidentally type:
>> if (1 = var)
>>because the compiler/interpreter sees that.

>>I used to do this, too. However I realized soon that you destroy the
> readability
>>and hence maintainability of your programs and in the end this hurts much
> more.
>>So I droped it.

> Speaking as a mathematician by training, when does  (x == 1) != (1 == x)?

In C++.  I generally use "constant == variable" in C and Awk,
primarily because I program in a wide array of languages,  many of
which use a single "=" for both setting and equality  (they
obviously don't support compound statements).  But I found in C++
I frequently ran into conversion problems,  so for C++ I go
the other way around.

--
Dan Mercer

- Show quoted text -

Quote:

> As for readability, if you can read APL or Forth, nothing in C etc. seems
> unreadable.

>>I don't know though why the fathers of C, AWK etc didn't just use the
>>mathematical correct way like Pascal/Delphi did it: always use ":=" for
> variable
>>assignments and "=" for ifs. This saved so many typos. But that's another
> topic.

> As you point out above, (1 = x) will catch typos quick enough. For me,
> Ritchie got it exactly right - assignment operations are far more frequent
> than equality testing, so make assignment operations (marginally) faster &
> easier. Besides, if one came from FORTRAN the equality comparison operation
> was .EQ. and assignment =. C just simplified the equality comparison
> operator. Not that the Pascal way doesn't introduce some rather obscure bugs
> itself (see Kernighan's classic essay 'Why Pascal Is Not My Favorite
> Programming Language').

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


Fri, 02 Jan 2004 10:49:32 GMT  
 
 [ 12 post ] 

 Relevant Pages 

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

2. Shadowing of variables broken in "switch"

3. Question on "switch" statement

4. Switch: how to match a "-"

5. "case" statement

6. sysStemSort "I" case fails

7. Help: Case of the Wandering "GETS"

8. "case" statement

9. Be Careful With "Use Cases"

10. "Cradle-to-Grave" CASE tools

11. Bug?: "glob" case sensitive on Windows

12. question about signal assignment in "case"

 

 
Powered by phpBB® Forum Software