The code you've written so far has a few problems, and it's taking you in the wrong direction -- away from using XML parsing methods and towards the much more troublesome and unreliable approach of using regex matches on XML data.

(Sure, regex matches on XML data seem "easier" at first, but in the long run, they aren't. Note that two XML files can differ drastically in line count and white space content, despite containing the exact same set of information. XML parsing handles this variation automatically, while line-oriented regexes tend to choke on it. Also, there are cases where the ordering of XML elements may vary, yet the data content would still be considered as "the same".)

Of course, if you are really, completely sure and confident that white-space / line-break patterns in your XML data will never change from the sample data you've shown, then a regex solution would probably suffice.

Since you don't have  use strict; you may have missed the fact that you are loading  @files with file names, but then using  @xml to run your "foreach" loop. You're also missing a semi-colon where you need one.

After you fix that, you'll want to declare the hash that will hold your data (do this before the foreach loop over the files), and then in the block that matches the "it_size" element, you assign the hash element. Here's how it probably should look (not tested):

#!/usr/bin/perl use strict; use Data::Dumper 'Dumper'; my @files = glob('/abc*/info.xml'); my %hash; my $info_name; foreach my $xmlname(@files) { open XML, $xmlname or die "Cannot open $xmlname for reading: $!\n" +; while(<XML>){ if ( /\<info_name\>/i ) { ($info_name) = (/<info_name>([^<])/i); } if ( /\<it_size\>/i ) { my ($it_size) = (/<it_size>([^<])/i); $hash{$info_name} = $it_size; } } } print Dumper( \%hash );
(updated code to remove residual use of "$line", and to add parens for regex assignments to work right)

Note that I've simplified the regexes a bit.

One last thing that would be worth checking: might there be any duplicate "info_name" values scattered within or across your set of XML files? If so, does it matter that some "it_size" values may be lost (over-written) in the process?


In reply to Re: Parse XML with Perl regex by graff
in thread Parse XML with Perl regex by ad23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.