Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

dbf2mysql parameter, WWWXXXAPC, wwwxxxAPC, How to unsubscrube from dategen spam, docmd.close 2585, WWWXXXDOCO, nu vot, dhcpd lease file "binding state", WWWXXXDOCO, how to setup procmail to process html2text

Links

XODOX
Impressum

#1: Not an ARRAY reference

Posted on 2005-12-02 04:32:54 by 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>

Report this message

#2: Re: Not an ARRAY reference

Posted on 2005-12-02 04:39:00 by 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'.

Report this message

#3: Re: Not an ARRAY reference

Posted on 2005-12-02 04:49:31 by jtbutlerhvb

its a HASH

Report this message

#4: Re: Not an ARRAY reference

Posted on 2005-12-02 04:50:22 by 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 :-)

Report this message

#5: Re: Not an ARRAY reference

Posted on 2005-12-02 05:04:34 by jtbutlerhvb

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

Report this message

#6: Re: Not an ARRAY reference

Posted on 2005-12-02 16:48:18 by 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],$_->{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:

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

Report this message

#7: Re: Not an ARRAY reference

Posted on 2005-12-02 17:58:46 by 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 <<EOF;
> $_->{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:
>
> <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.

Report this message