The XML parsing in your code seems ... odd. Can you provide a sample of the XML input? Is it really XML-encoded CSV or does the input actually have structure? If I read the code correctly, your XML parsing is implicitly assuming that all of the XML fields will be in the same order in every record.

Maybe something like:

my @items; for my $record (@records) { my %item = (); for my $tag ($record->children) { $item{$tag->name} = $tag->text } push @items, {%item}; }

This gets rid of all of the $*_pos variables and directly converts XML to an array of hashes for further processing.

The series of regex substitutions can go in their own sub cleanup. Then change the relevant line to $item{cleanup $tag->name} = cleanup $tag->text assuming that cleanup is declared with the appropriate prototype.

sub cleanup ($) { my $work = shift; $work =~ s/\r|\n//g; # Cleanup Carraige Returns $work =~ s/, / /g; # Cleanup Comma Space $work =~ s/,/ /g; # Cleanup Comma $work =~ s/"//g; # Cleanup Parentheses $work =~ s/^\s+|\s+$//g; # Trim spaces $work =~ s/([^[:ascii:]]+)/unidecode($1)/ge; return $work; }

This will create some differences, for example, $data[$class_pos] is now $items[$n]{sys_class_name} after the XML parsing loop, where $n is an index into the @items array.


In reply to Re: Need help with complex hash of hashes. by jcb
in thread Need help with complex hash of hashes. by vlturner

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.