Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Code to 'wildcard' multiple files or another suggestion?

by bms9nmh (Novice)
on Apr 21, 2016 at 19:13 UTC ( [id://1161144]=perlquestion: print w/replies, xml ) Need Help??

bms9nmh has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to parse multiple xml files but i'm a bit stuck with something. The  $twig->parsefile("output_1.xml"); section of the below script work fine in parsing the file stated. However, I would like to parse all of the .xml files in the direcotry. I have tried writing each file into the code like so:  $twig->parsefile("output_1.xml" "output_2.xml" "output_3.xml"); And i've also tried using a wildcard like I would in bash like so :  $twig->parsefile("*.xml"); But none of these attempts work. How can I change the script so that all of the .xml files in the directory are parsed by the script? I appologise fr my lack of knowledge, i've only just started using Perl.
#!/bin/perl -w use strict; use XML::Twig; my $twig = XML::Twig->new( twig_handlers => {item => \&acct} ); $twig->parsefile("output_1.xml"); sub acct { my ($t, $elt) = @_; for my $tag (qw(itemId title viewItemURL convertedCurrentPri +ce)) { print $elt->field($tag), "\n"; } print "\n"; print "\n"; }

Replies are listed 'Best First'.
Re: Code to 'wildcard' multiple files or another suggestion?
by toolic (Bishop) on Apr 21, 2016 at 19:25 UTC
      Excellent that worked a treat, thanks for your help.
Re: Code to 'wildcard' multiple files or another suggestion?
by Anonymous Monk on Apr 21, 2016 at 19:19 UTC
    $twig->parsefile("output_1.xml"); $twig->parsefile("output_2.xml"); $twig->parsefile("output_3.xml");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1161144]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found