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

Reading contents of file using regex

by jesuashok (Curate)
on Jan 31, 2006 at 10:37 UTC ( #526704=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:


I have a doubt, The following script is used to read the contents of a file (word by word (or) word preceeded by a number), check for the existence of the matched pattern which is done by the help of bookref() sub, if it is found then the matched pattern is prefixed with the tag . When I run the following script it goes to unending because it is not pointing to the next string after the complete execution of the loop . Please correct me

open IN, "r1.dat"; open OUT, ">r1.txt"; $/=undef; $line=; while ($line=~m/([123]?[.s]*[a-z\=.]+)/oi) { $book=$1; print $book,"n"; $t_book=$book; $t_book=~s/.//g; $t_book=~s/\l=([a-z]+)\/$1/ig; $t_book=~s/<(|/)(B|I|SC|U)>//ig; $quote=bibref($t_book); if($quote) { $line=~s/($book)/$1/ig; } } print OUT $line; close(IN);
Thanks in advance for the help.

"Keep pouring your ideas"

2006-10-07 Unapproved by planetscape once evidence of habitual plagiarism uncovered.

Replies are listed 'Best First'.
Re: Reading contents of file using regex
by GrandFather (Sage) on Jan 31, 2006 at 10:52 UTC

    Sigh - use strict; use warnings;, post a complete sample that demonstrates the problem. As usual, here's a starting point:

    use strict; use warnings; while (my $line = <DATA>) { ... } __DATA__ ...

    Dodgy stuff in your first post:

    $line=; # Just plain wrong! while ($line=~m/([123]?[.s]*[a-z\=.]+)/oi) # Where does $line get a va +lue? $t_book=~s/\l=([a-z]+)\/$1/ig; # The \/ is wrong! $t_book=~s/<(|/)(B|I|SC|U)>//ig; # The |/ is wrong (and why would you +want to capture it?)

    DWIM is Perl's answer to Gödel
Re: Reading contents of file using regex
by bart (Canon) on Jan 31, 2006 at 11:09 UTC
    You've got one obvious error, and that maybe due to copying from a HTML page:
    should be

    Furthermore, it might be a good idea to add the /g flag to your regex in the while condition. That way, it won't try to match again and again from the start, but instead it'll continue next time where it left off last time.

    while ($line=~m/([123]?[.s]*[a-z\=.]+)/ig)
    Oh, the /o is useless, you don't have a variable in your regex. So I dropped it.
Re: Reading contents of file using regex
by prasadbabu (Prior) on Jan 31, 2006 at 11:06 UTC

    jesuashok there are some problems in your code.

    You have not assigned any vales to $line

    I don't know what are you doing in the below line, which is not necessary. Suppose you need to count the number of occurences, then it might be useful.


    Also change the below line



    $t_book=~s/<(\/)?(B|I|SC|U)>//ig; #to delete both opening and closing tags of bold italics etc.

    GrandFather has highlighted clearly the other problems. Unless you give exact requirement you cannot get correct solutions. You will get answers only in assumptions.


Re: Reading contents of file using regex
by Hena (Friar) on Jan 31, 2006 at 11:24 UTC
    Also removing all characters in the $t_book '$t_book=~s/.//g;' will not help when doing the matching later. I think you think meant to do this '$t_book=~s/\.//g;'?
Re: Reading contents of file using regex
by chargrill (Parson) on Oct 07, 2006 at 03:45 UTC

    jesuashok, if your name is Anand Kumar*, it looks like this question was answered for you a few weeks before you asked it here.

    Is this you?

    * I've later learned that jesuashok signed some of his earlier posts as "Anthony Jesu Ashok", so I'm pretty confident that his name isn't Anand Kumar.

    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://526704]
[Corion]: Yay, Friday!

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-07-28 06:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (424 votes). Check out past polls.