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!

foreach problem 1

Status
Not open for further replies.

preethib

Programmer
Jul 20, 2000
39
IN
Hi,

I have a problem stripping lines of an xml file.
I have a xml file, were I want to strip of individual tags from the file. My input xml file has lines:

<feedid>2</feedid>
<pagetype>
<action>add</action>
<attribute>freehosting</attribute>
<id>48611</id>
<countryname>
<name>CA</name>
<name>DE</name>
<name>IT</name>
<name>UK</name>
</countryname>
</pagetype>
<pagetype>
<action>add</action>
<attribute>dadsagain</attribute>
<id>29905</id>
<countryname>
</countryname>
</pagetype>
<pagetype>
<action>add</action>
<attribute>trucksboerse</attribute>
<id>48613</id>
<countryname>
<name>DE</name>
</countryname>
</pagetype>
...

My output should skip the attrbutes with no country names and must look like this:
&quot;
ATTRIBUTE: freehosting
NAME: CA, DE, IT ,UK

ATTRIBUTE: trucksboerse
NAME: DE
&quot;
My code is:
# Process the xml feed file
foreach $line (@epifeed)
{

next if ($line =~ m/\<\?xml/);
next if ($line =~ m/pagetype\>/);
next if ($line =~ m/\action\>/);
next if ($line =~ m/page\>/);
next if ($line =~ m/countryname\>/);

if ($line =~ m/\<feedid\>(.*)\<.*$/) {
print Out_customerid_url &quot;\<B\>FEED ID: $1\<\/B\>\n\n&quot; ;
}


if ($line =~ m/\<attribute\>(.*)\<\/.*$/) {
$prev_url = $1;
}

if ($line =~ m/\<name\>(.*)\<\/.*$/) {
if ($1 eq &quot;&quot;) {next ;}
else{
print Out3 &quot;\nURL: $prev_url\n&quot; ;
print Out3 &quot;NAME: $1\n&quot;;}
}

}
foreach reads eachline of the file, but the problem I have is to be able to print all country names in a single line instead of printing single country name for the attribute many times , my current output is:
&quot;
ATTRIBUTE: freehosting
NAME: CA

ATTRIBUTE: freehosting
NAME: DE

ATTRIBUTE: freehosting
NAME: IT
&quot;
I want to print it as:

&quot;
ATTRIBUTE: freehosting
NAME: CA, DE, IT ,UK
&quot;

How can I do this? Any help will be appreciated.

Thanks,

 
I would take a different approach to this problem. The default input record separator is a new line character. So a line is a record. This problem is much simpler if there was a record for each <pagetype>. This can be done by changing the default input record separator &quot;$/&quot; to <pagetype>. Here's one way to do it:
Code:
$/ = '<pagetype';
open(F, &quot;file.txt&quot;) or die &quot;Can't open file.txt: $! \n&quot;;
@Lines = <F>;
close(F);
$/ = &quot;\n&quot;;
foreach $Line (@Lines) {
    next unless ($Line =~ m|<attribute>(.+)</attribute>|);
    print &quot;ATTRIBUTE: $1 \n&quot;;
    (@List) = $Line =~ m|<name>(\w+)</name>|g;
    $Countries = join(', ', @List);
    print &quot;NAME: $Countries \n&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top