in reply to Return values of tags which have the same name, in an array

I am unable to store these in an array.

What trouble are you having with this exactly?

You've only shown a snippet of code that can't be run because it's missing lots of things needed to compile, and also no sample input data or the expected output for that input (or any error messages you may be getting, copied exactly). Please see How do I post a question effectively? and Short, Self-Contained, Correct Example. If you provide something we can run, we'll be able to help much more efficiently.

Perhaps you are looking for push?

  • Comment on Re: Return values of tags which have the same name, in an array

Replies are listed 'Best First'.
Re^2: Return values of tags which have the same name, in an array
by Anonymous Monk on Aug 06, 2018 at 14:13 UTC

    Thanks for your quick reply, sorry for lack of information. I have added the full code below. I am trying to get the values of author into an array. Unfortuantely  my @array = map{$_->textContent} $authors->findnodes('./authors/author'); does not do this. Hopefuly this makes more sense

    use CGI qw(-utf-8 :all *table); use LWP::Simple qw(get); use URI; use XML::LibXML; use Data::Dumper; use utf8; binmode(STDOUT, ":encoding(utf-8)"); my $parser = XML::LibXML->new(); $author = param('query'); $maxHits = param('maxHits'); my $url = URI->new("http://www.dblp.org/search/api/?"); my($q,$h,$c,$f,$format) = ($author,$maxHits,"4","0","xml"); $url->query_form( 'q' => $q, 'h' => $h, 'c' => $c, 'f' => $f, 'format' => $format, ); print header(-charset=>'utf-8'), "\n", start_html({-title=>'DBLP Search', -author=>'<>'}), "\n"; if (param('submit')) { my $xml= get($url); my $doc = $parser->parse_string( $xml); print h1("Authors"), "\n"; foreach my $authors ($doc->findnodes('/result/hits/hit/info')) { print $_->textContent, "\n" for $authors->findnodes('./authors +/author'); my @array = map{$_->textContent} $authors->findnodes('./author +s/author'); } } print h1("DBLP Query"), "\n"; print start_form({-method=>"POST" -action=>"http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrap/u6ed/stat.pl"}) +; print label("query: "); print textfield({-name=>'query', -size=>200}), "\n"; print label("maxHits: "); print textfield({-name=>'maxHits', -size=>200}), "\n"; print br(), "\n"; print submit({-name=>'submit', -value=>'Submit'}), "\n"; print end_form, end_html

    XML file:

    <?xml version="1.0" encoding="UTF-8"?> <result> <query id="192448">Aderghal*</query> <status code="200">OK</status> <time unit="msecs">1.15</time> <completions total="1" computed="1" sent="1"> <c sc="5" dc="5" oc="5" id="19115731">aderghal</c> </completions> <hits total="5" computed="5" sent="5" first="0"> <hit score="1" id="80402"> <info><authors><author>Karim Aderghal</author><author>Alexander Khvost +ikov</author><author>Andrei Krylov</author><author>Jenny Benois-Pinea +u</author><author>Karim Afdel</author><author>Gwenaelle Catheline</au +thor></authors><title>Classification of Alzheimer Disease on Imaging +Modalities with Deep CNNs Using Cross-Modal Transfer Learning.</title +><venue>CBMS</venue><pages>345-350</pages><year>2018</year><type>Conf +erence and Workshop Papers</type><key>conf/cbms/AderghalKKBAC18</key> +<doi>10.1109/CBMS.2018.00067</doi><ee>https://doi.org/10.1109/CBMS.20 +18.00067</ee><url>https://dblp.org/rec/conf/cbms/AderghalKKBAC18</url +></info> <url>URL#80402</url> </hit> </hits> </result>
      Unfortuantely my @array = map{$_->textContent} $authors->findnodes('./authors/author'); does not do this.

      What makes you say that? You don't use @array anywhere else in the code.

      You don't use strict or warnings in that code though, and @array is scoped to the for loop. This means that if you try to use it later, perl won't complain about the variable not existing, but the array from inside the loop will have already been deleted.

      In addition to what Eily wrote, your XML parsing code works fine for me:

      As I said, perhaps you're looking for push?

      my @array; foreach my $authors ($doc->findnodes('/result/hits/hit/info')) { push @array, map {$_->textContent} $authors->findnodes('./authors/author'); } # use @array here