Not an ARRAY reference

J

jtbutlerhvb

I am reading an XML file in perl and looping through the items and
manipulating data. If I have 2 or more 'menuitems' in my xml file it
works ok. if i only have one like the code below I get "Not an ARRAY
reference" at the foreach line - any ideas why?

$create_menu = new XML::Simple (KeyAttr=>[]);

$data = $create_menu->XMLin($xml_file);

foreach $e (@{$data->{menuitem}}){
......
}


xml is
<?xml version='1.0'?>
<menu>
<menuitem>
<node>1</node>
<parent>1</parent>
<sibling>1</sibling>
<submenu>0</submenu>
<subsubmenu>0</subsubmenu>
<label>ramooooooooooon</label>
<link>topic1.xml</link>
</menuitem>
</menu>
 
I

it_says_BALLS_on_your_forehead

I am reading an XML file in perl and looping through the items and
manipulating data. If I have 2 or more 'menuitems' in my xml file it
works ok. if i only have one like the code below I get "Not an ARRAY
reference" at the foreach line - any ideas why?

$create_menu = new XML::Simple (KeyAttr=>[]);

$data = $create_menu->XMLin($xml_file);

foreach $e (@{$data->{menuitem}}){
.....
}

see the 'ref' function.

my $type = ref( $data->{menuitem} );
print "$type\n";

my guess is, $type ne 'ARRAY'.
 
J

John Bokma

I am reading an XML file in perl and looping through the items and
manipulating data. If I have 2 or more 'menuitems' in my xml file it
works ok. if i only have one like the code below I get "Not an ARRAY
reference" at the foreach line - any ideas why?

$create_menu = new XML::Simple (KeyAttr=>[]);

ForceArray => 1 ?
 
J

jtbutlerhvb

ForceArray => 1 worked - and I changed my manipulation to handle
arrays. thanks
 
A

anthony

Hello.
I am trolling through the message boards for a particular problem I am
having and came across this post - which uses the same perl module -
XML::Simple. I was wondering whether you could help me on how I can
extract a certain element and do some pattern matching on it before
writing all data as well as my pattern match value to a csv file?
For instance, my code is like this:
my $req = new HTTP::Request(GET =>
"http://www.somewhere.com/xml/sun.xml");
my $xmlfile = $ua->request($req);
if ($xmlfile->is_success)
{
$xmlcontent = $xmlfile->content;
# Read in the XML file and return the data
structure.
my $struct = $simple->XMLin($xmlcontent, forcearray
=> 1, keeproot => 1, noattr => 1, SuppressEmpty => 1 );
open(FILE, ">sol.csv") ||
die "Can't Open file: $!\n";
foreach (
@{$struct->{feed}->[0]->{record}})
{
print FILE <<EOF;
$_->{id}->[0],$_->{vendorid}->[0],$_->{severity}->[0],$_->{arch}->[0],$_->{os}->[0],$_->{osver}->[0],$_->{obsoletes}->[0],$_->{desc}->[0]
EOF
}
close (FILE);
} else {
print "Site not responding.";
}
the XML file would be contain entries like this:

<record id="2220" action="add">
<id>MSS-OAR-E01-2002:444.1</id>
<vendorid>104010-02</vendorid>
<date>2002-07-11</date>
<severity>M</severity>
<arch>sparc</arch>
<os>SunOS</os>
<osver>2.5.1</osver>
<obsoletes></obsoletes>
<desc>Sun(sm) Alert Notification 45707: Buffer overflow in
vold(1M)</desc>
</record>

What the code does is produce a the following line for the above entry:
MSS-OAR-E01-2002:444.1,104010-02,M,sparc,SunOS,2.5.1,,Sun(sm) Alert
Notification 45707: Buffer overflow in vold(1M)

What I want to be able to do is to extract the number from the
<desc></desc> element and place this into a separate field in my CSV
file like this:
MSS-OAR-E01-2002:444.1,104010-02,M,sparc,SunOS,2.5.1,,Sun(sm) Alert
Notification 45707: Buffer overflow in vold(1M),45707

Could you help me on this?
 
J

J. Gleixner

anthony said:
Hello.
I am trolling through the message boards for a particular problem I am
having and came across this post - which uses the same perl module -

Please avoid posting a new/unrelated question as a reply. Post a
new question whose subject relates to your issue.
print FILE <<EOF;
$_->{id}->[0],$_->{vendorid}->[0],$_->{severity}->[0],$_->{arch}->[0],$_->{os}->[0],$_->{osver}->[0],$_->{obsoletes}->[0],$_->{desc}->[0]
EOF
the XML file would be contain entries like this:

<record id="2220" action="add"> ....
<desc>Sun(sm) Alert Notification 45707: Buffer overflow in
vold(1M)</desc>
</record>

What the code does is produce a the following line for the above entry:
MSS-OAR-E01-2002:444.1,104010-02,M,sparc,SunOS,2.5.1,,Sun(sm) Alert
Notification 45707: Buffer overflow in vold(1M)

What I want to be able to do is to extract the number from the
<desc></desc> element

Using your code...

my $num = $1 if $_->{'desc'}->[0] =~ /(\d+):/;
and place this into a separate field in my CSV
file like this:
MSS-OAR-E01-2002:444.1,104010-02,M,sparc,SunOS,2.5.1,,Sun(sm) Alert
Notification 45707: Buffer overflow in vold(1M),45707

Could you help me on this?

print FILE
"$_->{id}->[0],$_->{vendorid}->[0],$_->{severity}->[0],$_->{arch}->[0],$_->{os}->[0],$_->{osver}->[0],$_->{obsoletes}->[0],$_->{desc}->[0],$num\n";

I sure hope your fields don't contain any ",", if so you might want to
look at Text::CSV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top