Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Check tag value

by Anonymous Monk
on Apr 08, 2004 at 11:29 UTC ( #343578=perlquestion: print w/replies, xml ) Need Help??

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

Hey everyone I have a quick question on perl, I have a list of files which contain the following tag <NumberofDays>. What I want to do is write a small perl script that would parse those list of files and check the value of that tag, and if the value of that tag is not a number then copy it into a different directory. like eg
<NumberofDays>23</NumberofDays> this okay leave it <NumberofDays>Two</NumberofDays> copy this file to a diff dir <NumberofDays>3d</NumberofDays> copy this file to a diff dir as well
I hope I explained it clearly

Thanks a bunch for your help

edit (broquaint): fixed formatting

Replies are listed 'Best First'.
Re: Check tag value
by esskar (Deacon) on Apr 08, 2004 at 11:38 UTC
    well, the parsing would be
    but i belive in Learning By Doing, so why don't u post what you have tried so far solving your problem and we will help you moving you into the right direction.

    bye, bye!
      This is what I have so far ..I am still working on it #!/usr/bin/perl my $dir = "/tmp/asheesh"; my gdir = "$dir/temp"; opendir (DIR, $dir) or die "can't open dir"; @files = readdir(DIR); closedir DIR; foreach $files(@files) { unless ( $files eq "." || $files eq "..") { open (FILE, "$files") or die "can't open file"; @Lines=<FILE>; close FILE; foreach $Lines(@Lines) { chomp $Lines; if ( $Lines eq "<NumberofDays>\d+</NumberofDays>" )
        Hi, you're code:
        if ( $Lines eq "<NumberofDays>\d+</NumberofDays>" )
        should be something like:
        if ( $Lines =~ m|<NumberofDays>(\d+)</NumberofDays>| )
        3 differences between your and my line:
        • eq vs. =~
        • to compare against a regular expression you use =~
        • the regular expression is in 'm|' and '|' characters
        • This is to make clear you are using a regular expression
        • the \d+ is in '(' and ')' characters
        • This is used to 'capture' the value of the expression inbetween them. In this case it would capture the value in $1. The rule is the first ()-match is captured in $1, the second in $2 etc. So in your code you could continue with something like
          print $1
        Hope this helps getting you started. For documentation on regular expressions there are 2 good tutorials that come with perl that were not mentioned here: perlrequick perlretut
        You need a regular expression in your if-test. Start by doing:
        perldoc perl
        at a command prompt. You'll notice if you page down a little that there are these 2 perldocs listed for regular expressions:
        perlre Perl regular expressions, the rest of the story perlreref Perl regular expressions quick reference
        So, if you want to read the 1st one, you would do
        perldoc perlre
        at a command prompt. The 1st response to your question gave you a regular expression to try. Read the perldocs, try a regular expression, and post back if you still have problems.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2023-06-05 00:02 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.