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

using regexp in a foreach loop

Status
Not open for further replies.

axnpictures

Programmer
Dec 16, 2009
3
0
0
RO
I want to print a regexp variable inside a loop from an xml file.
I'm reading the xml file through http.
This an example of my xml file:
<name>foo</name>
<name>foo1</name>

I'm using this tcl code:
foreach line $html {
regexp "<(name)>(.*?)</name>" $line dummy tag name
}
The problem is, I can only print $name outside the foreach loop and when I do, only the last line is shown.
If I try to print it inside the loop, I get "can't read $name no such variable".
I need to print it inside the loop in order to show all the xml document's lines.
How can I do that?

Thanks,
Alin
 
Hi

Code:
[b]set[/b] html [green][i]"<name>foo</name>[/i][/green]
[green][i]<name>foo1</name>"[/i][/green]

[b]foreach[/b] line [navy]$html[/navy] [teal]{[/teal]
  [b]regexp[/b] [green][i]"<(name)>(.*?)</name>"[/i][/green] [navy]$line[/navy] dummy tag name
  [highlight][b]puts[/b] [green][i]"In line '$line' the name is '$name'"[/i][/green][/highlight]
[teal]}[/teal]
Code:
In line '<name>foo</name>' the name is 'foo'
In line '<name>foo1</name>' the name is 'foo1'
How could that not work ?

Feherke.
 
Hmmm, well, I was about to say "I don't know it just won't work".
But your example works. Now, I removed some lines from my xml file. The <?xml version... one and the <data></data> tags.
Inside those tags are the <name></name> ones. Now it's working! It was working before as well but not inside the loop... and I'm confused now.
This is (was) the complete xml document:
Code:
<?xml version="1.0" standalone="yes" ?>
<data>
<name>foo</name>
<message>something</message>
<name>foo1</name>
<message>something else</message>
</data>
Now I only have the name and message tags and everything seems to be ok. Not sure what the problem was though...

Thanks for your help.
 
Hi

Code:
[b]set[/b] html [teal]{[/teal][teal]<?[/teal]xml version[teal]=[/teal][green][i]"1.0"[/i][/green] standalone[teal]=[/teal][green][i]"yes"[/i][/green] [teal]?>[/teal]
[teal]<[/teal]data[teal]>[/teal]
[teal]<[/teal]name[teal]>[/teal]foo[teal]</[/teal]name[teal]>[/teal]
[teal]<[/teal]message[teal]>[/teal]something[teal]</[/teal]message[teal]>[/teal]
[teal]<[/teal]name[teal]>[/teal]foo1[teal]</[/teal]name[teal]>[/teal]
[teal]<[/teal]message[teal]>[/teal]something [b]else[/b][teal]</[/teal]message[teal]>[/teal]
[teal]</[/teal]data[teal]>[/teal][teal]}[/teal]

[b]foreach[/b] line [navy]$html[/navy] [teal]{[/teal]
  [highlight][b]if[/b] [teal]{[/teal][teal][[/teal][/highlight] [b]regexp[/b] [green][i]"<(name)>(.*?)</name>"[/i][/green] [navy]$line[/navy] dummy tag name [highlight][teal]][/teal][teal]}[/teal] [teal]{[/teal][/highlight]
    [b]puts[/b] [green][i]"In line '$line' the name is '$name'"[/i][/green]
  [highlight][teal]}[/teal][/highlight]
[teal]}[/teal]
Code:
In line '<name>foo</name>' the name is 'foo'
In line '<name>foo1</name>' the name is 'foo1'

Feherke.
 
One more question...
Using the regular expression above, I can't show the messages from the message tag if there is more than one word inside the tags separated with space.
For example <message>Message goes here</message> won't be shown with "<(message)>(.*?)</message>".
 
Hi

That is because you just let Tcl to handle your string as a list. So it was split up on whitespaces. ( Running a simple [tt]foreach line [navy]$html[/navy] [teal]{[/teal] puts [navy]$line[/navy] [teal]}[/teal][/tt] and/or taking a look at thread287-1580627 would be answered your question faster than waiting for me... )

Just [tt]split[/tt] the string yourself and will work :
Code:
[b]set[/b] html [teal]{[/teal][teal]<?[/teal]xml version[teal]=[/teal][green][i]"1.0"[/i][/green] standalone[teal]=[/teal][green][i]"yes"[/i][/green] [teal]?>[/teal]
[teal]<[/teal]data[teal]>[/teal]
[teal]<[/teal]name[teal]>[/teal]foo[teal]</[/teal]name[teal]>[/teal]
[teal]<[/teal]message[teal]>[/teal]something[teal]</[/teal]message[teal]>[/teal]
[teal]<[/teal]name[teal]>[/teal]foo1[teal]</[/teal]name[teal]>[/teal]
[teal]<[/teal]message[teal]>[/teal]something [b]else[/b][teal]</[/teal]message[teal]>[/teal]
[teal]<[/teal]message[teal]>[/teal]Message goes here[teal]</[/teal]message[teal]>[/teal]
[teal]</[/teal]data[teal]>[/teal][teal]}[/teal]

[b]foreach[/b] line [highlight][teal][[/teal][b]split[/b][/highlight] [navy]$html[/navy] [highlight][green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal][/highlight] [teal]{[/teal]
  [b]if[/b] [teal]{[/teal][teal][[/teal] [b]regexp[/b] [green][i]"<(name|message)>(.*?)</[/i][/green][lime][i]\\[/i][/lime][green][i]1>"[/i][/green] [navy]$line[/navy] dummy tag value [teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]puts[/b] [green][i]"In line '$line' the $tag is '$value'"[/i][/green]
  [teal]}[/teal]
[teal]}[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top