Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
If it ain't broke, there's no need to fix it. Right. Nevertheless, these lines jumped out at me:
1. Variable interpolationFirst, 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 loopsBut now to come to your foreach loop (inner bit modified as above):
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:
<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:
In fact, you can use Perl's built-in $_ variable to shorten that even further:
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 In reply to Re: Re: Search and Replace
by Not_a_Number
|
|