Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any Limitation on IF Statements ? 1

Status
Not open for further replies.

mr4711

Programmer
Nov 4, 2011
2
DE
Hi friends,

I do use some IF statements this way:


if (match($0," U ") ||
match($0," W ") ||
match($0,"Guatemala"))
{c_guatemala++; assigned="yes"};

if (match($0," L ") ||
match($0,"Honduras"))
{c_honduras++; assigned="yes"};

when I add a 40-some statement I get a syntax error. What is wrong with it, 40 statements seems not to be much for any programming language ?

Thank you, Michael
 
Hi
[ul]
[li]Post the exact error message.[/li]
[li]Tell us which Awk implementation's which version are you using.[/li]
[li]Post some sample input.[/li]
[li]Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.[/li]
[/ul]
In meantime you could reduce those conditions :
Code:
[b]if[/b] [teal]([/teal][fuchsia]/ U /[/fuchsia] [teal]||[/teal] [fuchsia]/ W /[/fuchsia] [teal]||[/teal] [fuchsia]/Guatemala/[/fuchsia][teal])[/teal]
  [teal]{[/teal]c_guatemala[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]

[b]if[/b] [teal]([/teal][fuchsia]/ L /[/fuchsia] [teal]||[/teal] [fuchsia]/Honduras/[/fuchsia][teal])[/teal]
  [teal]{[/teal]c_honduras[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]

[gray]# or[/gray]

[b]if[/b] [teal]([/teal][fuchsia]/ U | W |Guatemala/[/fuchsia][teal])[/teal]
  [teal]{[/teal]c_guatemala[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]

[b]if[/b] [teal]([/teal][fuchsia]/ L |Honduras/[/fuchsia][teal])[/teal]
  [teal]{[/teal]c_honduras[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]
But for speed considerations, better do not use regular expressions when not necessary ( especially when 40 of them is needed ) :
Code:
[b]if[/b] [teal]([/teal][b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]" U "[/i][/green][teal])[/teal] [teal]||[/teal]
    [b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]" W "[/i][/green][teal])[/teal] [teal]||[/teal]
    [b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]"Guatemala"[/i][/green][teal]))[/teal]
  [teal]{[/teal]c_guatemala[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]

[b]if[/b] [teal]([/teal][b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]" L "[/i][/green][teal])[/teal] [teal]||[/teal]
    [b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]"Honduras"[/i][/green][teal]))[/teal]
  [teal]{[/teal]c_honduras[teal]++;[/teal] [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green][teal]}[/teal]

Feherke.
 
Hi

By the way, I would definitely not write it that way. I prefer this :
Code:
[b]BEGIN[/b] [teal]{[/teal]
  m[teal][[/teal][green][i]"guatemala"[/i][/green][teal]]=[/teal][green][i]" U | W |Guatemala"[/i][/green]
  m[teal][[/teal][green][i]"honduras"[/i][/green][teal]]=[/teal][green][i]" L |Honduras"[/i][/green]
[teal]}[/teal]
[teal]{[/teal]
  [b]for[/b] [teal]([/teal]i [b]in[/b] m[teal])[/teal]
    [b]if[/b] [teal]([/teal][navy]$0[/navy][teal]~[/teal]m[teal][[/teal]i[teal]])[/teal] [teal]{[/teal]
      c_[teal][[/teal]i[teal]]++[/teal] 
      [navy]assigned[/navy][teal]=[/teal][green][i]"yes"[/i][/green]
      [b]break[/b]
    [teal]}[/teal]
[teal]}[/teal]
Other condition expressions are only added in the [tt]BEGIN[/tt] block. Accumulated values are available in the c_ array, for example as [tt]c_[teal][[/teal][green]"guatemala"[/green][teal]][/teal][/tt].


Feherke.
 
Great, thank you very much. you are perfect this helps and looks much better.
Thx again, cheers michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top