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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array printing and if statement 1

Status
Not open for further replies.

clemhoff

Technical User
Jul 10, 2007
19
FR
I'm stuck on a problem of array printing and a "if" condition . I tried different combinations with the 'for' loop but the result always returns empty. Also when I try to include the "else" statement, I invariably get an "illegal instruction" message.

Could you help me on this?

--- SAMPLE ---
<h1>
L'école de la chair
</h1>
<div>
</div>
<!-- /rubric -->
<div class="colcontent">
<div class="rubric">
<p>
Date de parution:
<span class="bold">
<a href='/books/agenda.html?week=1963-10-06'>6 octobre 1963</a>
</span>
<br />
</p><!-- End first block -->
<p>
Auteur <span class="bold"><a href='/authors/indiv_gen=90843.html' title='Yukio Mishima'>Yukio Mishima</a></span>
<br /></p>
... etc
Traduction:
<a href='/books/all/lang-5001/'>français</a>,
<a href='/books/all/lang-5002/'>anglais</a>,
<a href='/books/all/lang-5004/'>espagnol</a>,
<a href='/books/all/lang-5129/'>allemand</a>.
<br/>
--- END SAMPLE ---

--- Expected output ---
L'école de la chair
6 octobre 1963
français, anglais,espagnol,allemand.



Code:
/usr/bin/awk '
function striptags() { 
  return gsub(/<[^>]*>/,"")
}

BEGIN {
  n="#n/a"
}
/<[Hh]1>/{
  getline
  arr["title"]= $0
}
/week=[0-9]+/{
  striptags()
  arr["release"]=$0
}
# ... etc
/lang-[0-9]+/{
  striptags()
  arr["lang"]++
  lang[$0]
  #print $0 #<!-- for test only -->
}
# ... etc
END{
  print arr["title"]
  if (i=arr["release"]) print i; else print n
  # ... etc

  #print arr["lang"] #<!-- for test only -->
  [COLOR=red][b]if (arr["lang"] > 0)
    ???
  else 
  print n[/b][/color]

  if (i=arr["editor"]) print i; else print n
  # ... etc
}

Thank you.

 
Hi

I would put that [tt]awk[/tt] code into a file. Is easier to edit.
Code:
[gray]#!/usr/bin/awk -f[/gray]

[b]function[/b] [COLOR=darkgoldenrod]striptags[/color][teal]()[/teal] [teal]{[/teal]
  [b]return[/b] [COLOR=chocolate]gsub[/color][teal](/<[^>]*>/,[/teal][green][i]""[/i][/green][teal])[/teal]
[teal]}[/teal]

[red]BEGIN[/red] [teal]{[/teal]
  n[teal]=[/teal][green][i]"#n/a"[/i][/green]
[teal]}[/teal]
[teal]/<[[/teal]Hh[teal]][/teal][purple]1[/purple][teal]>/[/teal][teal]{[/teal]
  [COLOR=chocolate]getline[/color]
  arr[teal][[/teal][green][i]"title"[/i][/green][teal]]=[/teal] [navy]$0[/navy]
[teal]}[/teal]
[teal]/[/teal]week[teal]=[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+/[/teal][teal]{[/teal]
  [COLOR=darkgoldenrod]striptags[/color][teal]()[/teal]
  arr[teal][[/teal][green][i]"release"[/i][/green][teal]]=[/teal][navy]$0[/navy]
[teal]}[/teal]
[gray]# ... etc[/gray]
[teal]/[/teal]lang[teal]-[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+/[/teal][teal]{[/teal]
  [COLOR=darkgoldenrod]striptags[/color][teal]()[/teal]
[highlight][gray]#  arr["lang"]++[/gray][/highlight]
[highlight][gray]#  lang[$0][/gray][/highlight]
  lang[teal]=[/teal]lang [navy]$0[/navy]
  [gray]#print $0 #<!-- for test only -->[/gray]
[teal]}[/teal]
[gray]# ... etc[/gray]
[red]END[/red][teal]{[/teal]
  [COLOR=chocolate]print[/color] arr[teal][[/teal][green][i]"title"[/i][/green][teal]][/teal]
  [b]if[/b] [teal]([/teal]i[teal]=[/teal]arr[teal][[/teal][green][i]"release"[/i][/green][teal]])[/teal] [COLOR=chocolate]print[/color] i[teal];[/teal] [b]else[/b] [COLOR=chocolate]print[/color] n
  [gray]# ... etc[/gray]

  [gray]#print arr["lang"] #<!-- for test only -->[/gray]
[highlight][gray]#  if (arr["lang"] > 0)[/gray][/highlight]
[highlight][gray]#    print "???"[/gray][/highlight]
[highlight][gray]# else[/gray][/highlight]
[highlight][gray]#  print n[/gray][/highlight]
  [highlight][COLOR=chocolate]print[/color] lang[teal]?[/teal]lang[teal]:[/teal]n[/highlight]

  [b]if[/b] [teal]([/teal]i[teal]=[/teal]arr[teal][[/teal][green][i]"editor"[/i][/green][teal]])[/teal] [COLOR=chocolate]print[/color] i[teal];[/teal] [b]else[/b] [COLOR=chocolate]print[/color] n

  [gray]# ... etc[/gray]
[teal]}[/teal]

Feherke.
 
It's actually much easier to stack the variable and shorten the logical condition like the way you did.
I'm really glad to have learned this technique.

Thank you

PS: Script editing is not a real problem because it is embedded in an AppleScript.

 
Hi

clemhoff said:
It's actually much easier to stack the variable and shorten the logical condition like the way you did.
Well, depends on your goal. For example, if you need to sort the language names alphabetically, you will definitely need your original array solution. Which was not far from success :
Code:
[gray]# (...)[/gray]

[teal]/[/teal]lang[teal]-[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+/[/teal][teal]{[/teal]
  [COLOR=darkgoldenrod]striptags[/color][teal]()[/teal]
  arr[teal][[/teal][green][i]"lang"[/i][/green][teal]]++[/teal]
  lang[teal][[/teal][highlight]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]][/teal][/highlight][teal]][/teal][highlight][teal]=[/teal][navy]$0[/navy][/highlight]
[teal]}[/teal]

[gray]# (...)[/gray]

  [b]if[/b] [teal]([/teal]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]][/teal] [teal]>[/teal] [purple]0[/purple][teal])[/teal] [highlight][teal]{[/teal][/highlight]
    [highlight][b]for[/b] [teal]([/teal]i[teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]];[/teal]i[teal]++)[/teal] [COLOR=chocolate]printf[/color] [green][i]"%s "[/i][/green][teal],[/teal]lang[teal][[/teal]i[teal]][/teal][/highlight]
    [highlight][COLOR=chocolate]print[/color] [green][i]""[/i][/green][/highlight]
  [highlight][teal]}[/teal][/highlight] [b]else[/b]
    [COLOR=chocolate]print[/color] n

[gray]# (...)[/gray]
Or abit shortened :
Code:
[gray]# (...)[/gray]

[teal]/[/teal]lang[teal]-[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+/[/teal][teal]{[/teal]
  [COLOR=darkgoldenrod]striptags[/color][teal]()[/teal]
  lang[teal][[/teal][highlight][teal]++[/teal][/highlight]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]]]=[/teal][navy]$0[/navy]
[teal]}[/teal]

[gray]# (...)[/gray]

  [b]if[/b] [teal]([/teal]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]][/teal] [teal]>[/teal] [purple]0[/purple][teal])[/teal]
    [b]for[/b] [teal]([/teal]i[teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]];[/teal]i[teal]++)[/teal] [COLOR=chocolate]printf[/color] [green][i]"%s[highlight]%s[/highlight]"[/i][/green][teal],[/teal]lang[teal][[/teal]i[teal]][/teal][highlight][teal],[/teal]i[teal]<[/teal]arr[teal][[/teal][green][i]"lang"[/i][/green][teal]]?[/teal][blue]OFS[/blue][teal]:[/teal][blue]ORS[/blue][/highlight]
  [b]else[/b]
    [COLOR=chocolate]print[/color] n

[gray]# (...)[/gray]
clemhoff said:
Script editing is not a real problem because it is embedded in an AppleScript.
Yes, but if you have it in a separate file, the editor will be able to highlight the [tt]awk[/tt] syntax.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top