Not an ARRAY reference

Discussion in 'Perl Misc' started by jtbutlerhvb@comcast.net, Dec 2, 2005.

  1. Guest

    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>
     
    , Dec 2, 2005
    #1
    1. Advertising

  2. 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'.
     
    it_says_BALLS_on_your_forehead, Dec 2, 2005
    #2
    1. Advertising

  3. Guest

    its a HASH
     
    , Dec 2, 2005
    #3
  4. John Bokma Guest

    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 :)
     
    John Bokma, Dec 2, 2005
    #4
  5. Guest

    ForceArray => 1 worked - and I changed my manipulation to handle
    arrays. thanks
     
    , Dec 2, 2005
    #5
  6. anthony Guest

    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?
     
    anthony, Dec 2, 2005
    #6
  7. J. Gleixner Guest

    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],$_->{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.
     
    J. Gleixner, Dec 2, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    12
    Views:
    719
    Howard
    Jul 21, 2004
  2. aurora
    Replies:
    6
    Views:
    396
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Oct 18, 2004
  3. Replies:
    1
    Views:
    482
    Mark Rae [MVP]
    Sep 20, 2007
  4. fatted
    Replies:
    2
    Views:
    149
    James Willmore
    Oct 23, 2003
  5. Andrew Poulos

    Using an array to reference a different array

    Andrew Poulos, Dec 22, 2008, in forum: Javascript
    Replies:
    6
    Views:
    86
    Jorge
    Dec 23, 2008
Loading...

Share This Page