Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Reading contents of file using regex
by GrandFather (Saint) 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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://526704]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2023-03-26 11:41 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.