in reply to remove line break from 1 line

Hi djbryson,

    Should I be doing this line by line instead of slurp?

Yes, I would do it line by line.  For one thing, your chomp statement is only being applied to the single line that you read in, rather than a list of lines.

For another, your regular expression is only being applied once, rather than to all of the lines individually.

But finally, if you want to debug what lines you're reading, and what they look like after any modifications, it's a LOT easier when you read them into an array.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: remove line break from 1 line
by Anonymous Monk on Jan 19, 2007 at 18:58 UTC
    ok, i've switched to line by line... but it's still not removing the line breaks.
    #!C:\Perl\bin\perl.exe use strict; use File::Find; use File::Slurp; use Time::Local; print "\nRunning find-slurp_search-within.pl... \n\n"; my $root = "C:/"; # use forward slash, you can use mapped drives. print "changing to $root\n"; chdir $root; my $no_switches=0; my $no_files=0; my @log = (); my $dir; # find (\&Wanted, "department", "managers", "mybranch", "mycity", "myi +nfo", "resources"); find (\&Wanted, "working"); #directories - comman delimited sub Wanted { print "*Processing: $root$File::Find::name \n"; if ($_ =~ /\.htm(l)?$/i) { open(xFILE,$_) or die "ERROR: couldn't open file"; my @file = <xFILE>; my $line; foreach $line (@file) { if ($line =~ m/(<meta[\s\r\n\t]+name="revision"[\s\r\n\t]+con +tent=$)/im) { print $line; chomp($line); close (xFILE); $no_switches++; } # end if } $no_files++; } #end if matches filetype else { print "file type not processed\n\n"; } #add $_ if you want + to see URL of file not processed. } # end sub # add timestamp, # files, # matches to log open(LOG, ">H:/Web/perl/log.txt") or die "ERROR: Can't open log.txt"; my $timestamp = localtime(); print LOG "$no_files files - $no_switches matches - $timestamp\n"; foreach(@log) { print LOG; } close (LOG); print "log file updated. $no_files files - $no_switches matches";
      Well, one thing that's immediately obvious is that you are only chomping if the regular expression matches.

      Why not just chomp all lines on input? ...

      chomp(my @file = <xFILE>);

      Another useful idiom allows you to combine my with your foreach statement:

      # No need to do # my $line; # foreach $line (@file) { # foreach my $line (@file) {

      So what do you get when you put a print of the lines after they're input?


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Sorry about the last 2 posts... somehow wasn't logged in. I don't want to remove other line breaks, only when the regex matches. EX:
        <meta name="revision" content= "Mon, 05 Jul 2006 23:59:59 GMT">
        I need to get rid of that line break. my output:
        changing to C:/ *Processing: C:/working file type not processed *Processing: C:/working/CIPP_en.html matched: <meta name="revision" content= *Processing: C:/working/Copy of CIPP_en.html matched: <meta name="revision" content= *Processing: C:/working/Copy of index_en.html *Processing: C:/working/index_en.html log file updated. 4 files - 2 matches
        aaah, i know why. It's chomping it in memory... i have to overwrite the file... ugh So, if i'm going line by line, chomp it. Now I need to rewrite the file right? How can i do that? I need the entire file contents to do that. Am I supposed to slurp and read line x line or just do a switch? ---- disregard, i get it now. I have to read it into an array and then write the array