Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

reading from a text file

by bory (Beadle)
on Jun 07, 2005 at 07:59 UTC ( [id://464178]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have a text file which looks exactly like this:
FIRST Level Description EQT.FIRST.Id: 2 EQT.FIRST.eqtID_01.allowedType: 4TEST EQT.FIRST.eqtID_02.allowedType: 5TEST SECOND Level Description
And from this file I want to read the values:01,02 and 4TEST,5TEST Here is my code but it doesn't work properly:
my $data_file = "test.txt"; open(FILE, "< $data_file"); while(<FILE>) { if ( /FIRST Level Description/.. /SECOND Level Description/ ) { if (/EQT.FIRST.eqtID_(\d+).allowedType:([A-Z0-9]+)/) { ($str1,$str2 ) = ($1,$2); } print "$str1 \n $str2"; }}
Thank you for your time.

Replies are listed 'Best First'.
Re: reading from a text file
by robartes (Priest) on Jun 07, 2005 at 08:05 UTC

    Your second regex does not match because of a space between the [45]TEST and and the rest of the line in the input. Try this regex:

    /EQT.FIRST.eqtID_(\d+).allowedType:\s+([A-Z0-9])+)/

    Notice the extra \s+ (match 1 or more whitespace characters).

    CU
    Robartes-

Re: reading from a text file
by ZlR (Chaplain) on Jun 07, 2005 at 08:11 UTC
    Also remember that the dot has the special meaning of "any character" in a regex, so while your code is working ( with robartes' correction ) it's not exactly doing what you think .

    Since you want to match real dots you should probably use \. in your expressions ( escaping the . metacharacter with \ will make it lose its special meaning)

Re: reading from a text file
by tchatzi (Acolyte) on Jun 07, 2005 at 08:41 UTC
    Try something like this (simple though)
    #!/usr/bin/perl -w use strict; open(F,"test.txt"); foreach(<F>){ if ($_ =~ /.*(\d\d).*(\d\w+)/){ print "$1 : $2 \n"; } } close F;


    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
      if ($_ =~ /.*(\d\d).*(\d\w+)/){
      I think you might have forgot that \w indeed accepts digits.
      
      -madasamy-
      
        No i didn't forget.
        But its either a '\d' inside the '()' like this (\d\w+)
        or a '\s' outside like this \s(\w+)
        Do you see any big difference?
        Cause if you don't use either of them and write it like this
        ($_ =~ /.*(\d\d).*(\w+)/)
        Then the only thing that $2 will have inside, will be a single 'T'.
        So i suggest you study a bit of regex more, before coming to conclusions

        ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
        The only thing you can take out is the first ' .* '
        Which i put there for educational reasons, cause the guy doesn't know too much of regex, so there is no need to give him a hard time, yet.

        ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
Re: reading from a text file
by aukjan (Friar) on Jun 07, 2005 at 08:11 UTC
    Also you might want to change '.' into '\.' to specifically match a '.' and not 'any character'. This will prevent any unsuspected behaviour.

    .:| If it can't be fixed .. Don't break it |:.

Log In?
Username:
Password:

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

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

    No recent polls found