Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Search and Replace

by nofernandes (Beadle)
on Aug 27, 2003 at 15:31 UTC ( [id://287067]=note: print w/replies, xml ) Need Help??


in reply to Search and Replace

Thank you all for your precious help once again.

I´ve made the following code that works

use strict; use warnings; my $path='D:\npodev\forms\src'; my @intervalos= ("[a-e]", "[f-h]", "[i-m]", "[n]", "[o-s]", "[t-z]"); my $esquema="RMS_MC"; my $start_flag = '<REANALYZED>'; my $stop_flag = '</REANALYZED>'; sub procura_ficheiros{ my ($path,$intervalo)=@_; opendir(DIR, $path) ||die "$!"; my @files=grep{/^$intervalo.*\.[f|m]/i} readdir(DIR); closedir (DIR); my $aux=0; foreach my $pos (@files){ $files[$aux]="\t\"".$path."\\".$files[$aux]."\""." $esquema\n"; $aux++; } return @files; } foreach my $intervalo (@intervalos){ my @ola=procura_ficheiros($path,$intervalo); my $replacement = join "", @ola; open ORIGINAL, "< teste.txt" or die "can't open original file\n"; my $original; { local $/; $original = <ORIGINAL>; } close ORIGINAL; $original =~ s/ $start_flag .*? (\s*) $stop_flag \n /$start_flag\n$replacement$1$stop_flag/gsx; open COPY, "> copy.txt" or die "can't write to copy\n"; print COPY $original; close COPY; system "hello.bat"; }

Thank you..

Nuno

Replies are listed 'Best First'.
Re: Re: Search and Replace
by Not_a_Number (Prior) on Aug 27, 2003 at 20:37 UTC

    If it ain't broke, there's no need to fix it. Right. Nevertheless, these lines jumped out at me:

    my $aux=0; foreach my $pos (@files){ $files[$aux]="\t\"".$path."\\".$files[$aux]."\""." $esquema\n"; $aux++; }

    1. Variable interpolation

    First, take the line:

    $files[$aux]="\t\"".$path."\\".$files[$aux]."\""."   $esquema\n";

    This reminds me of something else I saw recently, which (simplified) read something like this:

    my $line = $first . " " . $second . " ". $third;

    As if Perl didn't allow variable interpolation! Your line can be simplified to:

    $files[$aux]="\t\"$path\\$files[$aux]\"   $esquema\n";

    Further, by using qq(), you would no longer have to escape the double quotes:

    $files[$aux] = qq '\t"$path\\$files[$aux]"   $esquema\n';

    which I for one find a bit easier on the eye.

    2. for/foreach loops

    But now to come to your foreach loop (inner bit modified as above):

    my $aux=0; foreach my $pos (@files){ $files[$aux] = qq '\t"$path\\$files[$aux]" $esquema\n'; $aux++; }

    I'm far from being a Perl guru, but I think I may safely say that this is ugly :) You can do this far more efficiently in Perl:

    foreach my $pos ( 0 .. $#files ) { $files[$pos] = qq '\t"$path\\$files[$pos]" $esquema\n'; }

    <parenthesis>Occasionally, you might also come across some rather quaint variations of the above, along the lines of:

    foreach ( my $pos = 0; $pos < @files; $pos++ ) { ... }

    but it's best to forget these unless you want to increment your counter ($pos) by anything other than 1.</parenthesis>

    However, you would still not be using the full power of Perl. To quote Marc Jason Dominus: "Any time you have a C-like for loop that loops over the indices of an array, you're probably making a mistake." A more perlish way to do what you want would be:

    foreach my $file ( @files ) { $file = qq '\t"$path\\$file" $esquema\n'; }

    In fact, you can use Perl's built-in $_ variable to shorten that even further:

    for ( @files ) { # 'for' and 'foreach' are equivalent $_ = qq '\t"$path\\$_" $esquema\n'; }

    From which, to end this far longer than intially intended post, it is only a short step to using map:

    @files = map { qq '\t"$path\\$_"   $esquema\n' } @files;

    HTH

    dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found