Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How can I find a line in a RTF file?

by thanos1983 (Parson)
on Aug 08, 2014 at 01:43 UTC ( [id://1096704]=note: print w/replies, xml ) Need Help??


in reply to How can I find a line in a RTF file?

Hello Kevyt,

I am not really familiar with RTF Files but I created a few solutions that I think is exactly what you need.

I have tested them on a file test.rtf that I create. Based on what I see all solutions work fine.

Upsate

Forgot to wright, never forget to use the or die (function) when opening and closing files. They have saved me several times.

Small code modification updates
#!/usr/bin/perl use strict; use warnings; sub regex { my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open( my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; while ( <$in> ) { chomp($_); # Solution: 1 substring # if (index($_, $substr) != -1) { # Solution: 2 regex string{times,} at least string 1s # if( $_ =~ /(?:$substr){1,}/) { # Solution: 3 regex match string or more times: + # if( $_ =~ /(?:$substr)+/ ) { # Solution: 4 quotemeta match string if( $_ =~ /\Q$substr\E/ ) { chop($_); # I use chop to remove the last trailing character ( +}) print $_ . "\n"; push(@_,$_); next; } } # Count the number of signature lines print "I found the string: ".@_." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } regex(); sub my_grep { # Solution 5: grep in perl!!!! (My favorite) my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open(my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; @_ = <$in>; chomp @_; my @out = grep { $_ =~ /Date of Last Update:/ } @_; print "I found the string: ".@out." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } # &my_grep();

I hope this solves your problems.

Seeking for Perl wisdom...on the process...not there...yet!

Replies are listed 'Best First'.
Re^2: How can I find a line in a RTF file?
by Bethany (Scribe) on Aug 08, 2014 at 02:31 UTC

    I'm absent-minded, so instead of trying to remember to add a "die" to every operation that might need it, I just keep "use autodie;" in the standard boilerplate I put at the top of every script and module I write. For my purposes it does the trick fine.

    The autodie module and pragma on CPAN

Re^2: How can I find a line in a RTF file?
by Anonymous Monk on Aug 08, 2014 at 11:07 UTC

    A couple of suggestions for your code:

    1. Although it works, open(IN, ,"<", $file) has an extra comma in it. Also lexical filehandles are generally considered to be better (e.g. open my $in, '<', $file or die $!;)
    2. You use the array @_ in your subs for storing data. While that's possible, @_ is generally only used to access the arguments passed into a sub, and using it the way you're doing is very likely to confuse others working with your code.
    3. Although I'm not sure what your intentions were, I suspect the regex /Date of Last Update:+/ is not doing what you expect: It'll match the string "Date of Last Update" followed by one or more colons. If you want to match the string itself multiple times, you need a group: /(?:Date of Last Update:)+/, although I'm not sure if the input file will ever contain the string "Date of Last Update:Date of Last Update:". Also, {1,} is equivalent to +.
    4. You can use your variable $substr in your regular expressions, e.g. /\Q$substr\E/ (for the meaning of \Q...\E see quotemeta).
    5. Naming a sub the same as a Perl keyword, in this case "grep", is generally a bad idea - the only exception is when you're actually trying to replace the built-in grep - because it will create much confusion as to which function is supposed to be called when, both for the readers of your code and for Perl. This confusion is why you had to call your "grep" as &grep();. Also, inside your "grep", you're calling Perl's grep, any slight mistake in syntax may call your "grep" instead and create infinite recursion.
    6. A small hint: grep { $_ =~ /Date of Last Update:/ } can be written as grep { /Date of Last Update:/ } and "... file: ".$file.": $!\n"; can be written as "... file: $file: $!\n";
    7. On style: If you find yourself using $_ a lot, over multiple lines, then usually it's better to use a lexical variable instead. $_ is global and can (accidentally) be manipulated by other code, especially code that someone else inserts later. You can use something like while (my $line = <$in>) { instead.

      Hello Anonymous Monk,

      Well I am not an expert I consider my self just a beginner, so any suggestions for improvement are always welcome. :D

      Thank you for your time and effort to write these comments. :D

      Seeking for Perl wisdom...on the process...not there...yet!

Log In?
Username:
Password:

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

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

    No recent polls found