Not an ARRAY reference

Not an ARRAY reference

am 02.12.2005 04:32:54 von 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



1
1
1
0
0

topic1.xml

Re: Not an ARRAY reference

am 02.12.2005 04:39:00 von simon.chao

jtbutler...@comcast.net wrote:
> 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'.

Re: Not an ARRAY reference

am 02.12.2005 04:49:31 von jtbutlerhvb

its a HASH

Re: Not an ARRAY reference

am 02.12.2005 04:50:22 von John Bokma

jtbutlerhvb@comcast.net wrote:

> 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 ?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

Re: Not an ARRAY reference

am 02.12.2005 05:04:34 von jtbutlerhvb

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

Re: Not an ARRAY reference

am 02.12.2005 16:48:18 von 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 < $_->{id}->[0],$_->{vendorid}->[0],$_->{severity}->[0],$_->{a rch}->[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:


MSS-OAR-E01-2002:444.1
104010-02
2002-07-11
M
sparc
SunOS
2.5.1

Sun(sm) Alert Notification 45707: Buffer overflow in
vold(1M)



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
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?

Re: Not an ARRAY reference

am 02.12.2005 17:58:46 von glex_no-spam

anthony wrote:
> 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 < > $_->{id}->[0],$_->{vendorid}->[0],$_->{severity}->[0],$_->{a rch}->[0],$_->{os}->[0],$_->{osver}->[0],$_->{obsoletes}->[0 ],$_->{desc}->[0]
> EOF

> the XML file would be contain entries like this:
>
>
....
> Sun(sm) Alert Notification 45707: Buffer overflow in
> vold(1M)

>

>
> 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
> 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.