Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Search and Copy

by mikebailey (Initiate)
on Nov 23, 2012 at 16:35 UTC ( #1005298=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Search and Copy
in thread Search and Copy

This is what I have so far... open (OUTPUT, ">Results.txt") || die ("Could not open file results.txt; $OS_ERROR"); open( INFILE, "Textfile.txt" )or die("Can not open input file: $!"); while (<INFILE>) { if ($ARG =~ /Something/ ) { print OUTPUT $ARG ; } } close (OUTPUT);


Comment on Re^3: Search and Copy
Re^4: Search and Copy
by kennethk (Monsignor) on Nov 23, 2012 at 17:47 UTC

    Again, please use <code> tags and read How do I post a question effectively?

    I have found strict to be very helpful to identifying and avoiding bugs in my code -- see Use strict warnings and diagnostics or die. If I were going to write your posted code, it might look more like:

    use strict; use warnings; open (my $out, ">", "Results.txt") or die ("Could not open file Result +s.txt; $!"); open (my $in, "<", "Textfile.txt") or die ("Can not open input file: $ +!"); local $/; while (<$in>) { if (/(web site.{250})/i) { print $out $1; } }

    Changes that I made include:

    1. I swapped to lexical file handles and 3 argument open, which are considered better practice for a number of reasons. See perlopentut. In particular, this gives strict more power to help and removes the need for explicit close.
    2. I corrected inconsistency between your file name and error message; file names are generally case sensitive.
    3. I swapped to slurp mode using $/. Given the large number of characters you are interested in, it is unlikely they will all fall on the same line.
    4. Your while(<>) loop read data into $_ not $ARG, so I corrected that.
    5. I swapped your regular expression to the regular expression I posted above, with the addition of the s modifier. This makes it so . also matches new lines, and is essential when working in slurp mode.

    You may consider going to http://learn.perl.org to gather some learning resources before trying to run too far.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^4: Search and Copy
by mikebailey (Initiate) on Nov 24, 2012 at 21:29 UTC
    open (OUTPUT, ">Results.txt") open( INFILE, "Textfile.TXT" ) while (<INFILE>) { if ($ARG =~ /(Something.{0,250})/ ) { print OUTPUT $ARG ; } } close (OUTPUT);

    The "regexp" suggested is not working.

    Thoughts?

      The regex would work if you match it against the right variable. Where is $ARG coming from? Use strict and warnings; and if someone gives you a code, taking just a small part of it if you do not understand the rest would probably not work.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (17)
As of 2014-10-01 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (37 votes), past polls