-4

I Have a array that has stored data like this:

<WebPage>
<Action>Action Goes Here 1</Action>
<SystemData>SystemData Goes Here 1</SystemData>
<PageSatausData>PageSatausData Goes Here 1</PageSatausData>
<PageNameData>PageNameData Goes Here 1</PageNameData>
<TitleData>TitleData Goes Here 1</TitleData>
<KeywordData>KeywordData Goes Here 1</KeywordData>
<DescriptionData>DescriptionData Goes Here 1</DescriptionData>
<HeaderData>HeaderData Goes Here 1</HeaderData>
<BodyData>BodyData Goes Here 1</BodyData>
<FooterData>FooterData Goes Here 1</FooterData>
</WebPage>
<WebPage>
<Action>Action Goes Here 2</Action>
<SystemData>SystemData Goes Here 2</SystemData>
<PageSatausData>PageSatausData Goes Here 2</PageSatausData>
<PageNameData>PageNameData Goes Here 2</PageNameData>
<TitleData>TitleData Goes Here 2</TitleData>
<KeywordData>KeywordData Goes Here 2</KeywordData>
<DescriptionData>DescriptionData Goes Here 2</DescriptionData>
<HeaderData>HeaderData Goes Here 2</HeaderData>
<BodyData>BodyData Goes Here 2</BodyData>
<FooterData>FooterData Goes Here 2</FooterData>
</WebPage>

What I'm trying to do is loop thew it and assign variable to each of the values like this:

foreach my $Line (@Meta_Content) {

my($Var1,$Var2,$Var3,$Var4,$Var5,$Var6,$Var7,$Var8,$Var9,$Var10) = split (/\>\</,$Line,10);

print "Result: $Var1,$Var2,$Var3,$Var4,$Var5,$Var6,$Var7,$Var8,$Var9,$Var10<br>";
 }

With no luck I'm aware of the XML modules but in this case I need a regex to do so modules are not an option.

Blnukem
  • 173
  • 3
  • 13
  • 6
    Using regex to parse XML is going to result in lots of failures and wasted effort. Use a real XML parser. Really. – Jim Garrison Feb 12 '18 at 18:58
  • 3
    Using named variables for this is not a smart idea. In XML the order of elements is not relevant. It might change, and then your data gets assigned to the wrong variable. That's why the semantics of an XML parser are required. It's also not clear if your array contains two ``elements as one array element, or if each of them is one element in the array. – simbabque Feb 12 '18 at 19:05
  • 1
    Feel-free to write your own regexp-based parser, but don't waste our time if you want to reinvent the wheel. Besides, why is our code on SO better than our code on CPAN??? – ikegami Feb 12 '18 at 19:09
  • 1
    https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la https://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg – Matt Jacob Feb 12 '18 at 19:26

2 Answers2

-2

Instead of this:

my($Var1,$Var2,$Var3,$Var4,$Var5,$Var6,$Var7,$Var8,$Var9,$Var10) = split (/\>\</,$Line,10);

print "Result: $Var1,$Var2,$Var3,$Var4,$Var5,$Var6,$Var7,$Var8,$Var9,$Var10<br>";
 }

you can write:

my @pieces = split (/\>\</,$Line,10);
my $str = join '', @pieces;
print "Results: $str <br>";

And if you need to refer to the individual items, instead of writing $var1, you can write $pieces[0]; and instead of writing $var2, you can write $pieces[1], etc.

See how much more succinct that is? Beginners in every language try what you did. The rule is: if you ever find yourself writing variable names that only differ by a number, then you should store the data in an array instead.

7stud
  • 46,922
  • 14
  • 101
  • 127
  • 7stud Thank you! I'm glad to see there are some people here to help like yourself, then the others that just criticize you over your question. – Blnukem Feb 13 '18 at 15:21
-3

here it is

#!/usr/bin/perl
use strict; use warnings; use Data::Dumper;
my $hash;


while (<DATA>) {

    if ( /<WebPage>/ ) {
    $hash={} 
    }
    elsif  ( /<\/WebPage>/ ) {
    print Dumper $hash
    }
    elsif ( /^<(.+)>(.+)<\/\1>\s*/ ) {
    $hash->{$1}=$2      
    }
}

__DATA__
<WebPage>
<Action>Action Goes Here 1</Action>
<SystemData>SystemData Goes Here 1</SystemData>
<PageSatausData>PageSatausData Goes Here 1</PageSatausData>
<PageNameData>PageNameData Goes Here 1</PageNameData>
<TitleData>TitleData Goes Here 1</TitleData>
<KeywordData>KeywordData Goes Here 1</KeywordData>
<DescriptionData>DescriptionData Goes Here 1</DescriptionData>
<HeaderData>HeaderData Goes Here 1</HeaderData>
<BodyData>BodyData Goes Here 1</BodyData>
<FooterData>FooterData Goes Here 1</FooterData>
</WebPage>
<WebPage>
<Action>Action Goes Here 2</Action>
<SystemData>SystemData Goes Here 2</SystemData>
<PageSatausData>PageSatausData Goes Here 2</PageSatausData>
<PageNameData>PageNameData Goes Here 2</PageNameData>
<TitleData>TitleData Goes Here 2</TitleData>
<KeywordData>KeywordData Goes Here 2</KeywordData>
<DescriptionData>DescriptionData Goes Here 2</DescriptionData>
<HeaderData>HeaderData Goes Here 2</HeaderData>
<BodyData>BodyData Goes Here 2</BodyData>
<FooterData>FooterData Goes Here 2</FooterData>
</WebPage>