OK, if anyone else stumbles upon this page, here's what I came up with...Apparently, $datasource is a reference to an array. So you can't just change the $ to an @. You have to use curly braces like this to read into the reference:

$element = scalar(@{$datasource});

My code now reads:

1 #!/usr/bin/env perl 2 3 use strict; 4 use warnings; 5 #use IO::Socket; 6 use Data::Dumper; 7 use XML::Simple; 8 my $xs1 = XML::Simple->new(); 9 my $file = $ARGV[0]; 10 my $doc = $xs1->XMLin($file, forcearray => 1); 11 foreach my $datasource ($doc->{DATASOURCE}){ 12 my $length = scalar(@{$datasource}); 13 print $length ."\n"; 14 foreach my $element (@{$datasource}){ 15 print Dumper($element); 16 # my $element = scalar(@datasource); 17 # print "Element = $element\n"; 18 # for(my $element=0; $element < scalar($datasource); $element++){ 19 # print $datasource[$element]; 20 } 21 }

Each $element is a reference to a hash now. I'll have to de-reference those to access them as well, I'm sure.


In reply to Re^2: Strange Behavior with XML::Simple when pulling info out of an XML file by lunchlady55
in thread Strange Behavior with XML::Simple when pulling info out of an XML file by lunchlady55

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.