Hello, victorz22. I am writing this in hopes that you actually want to get better at using Perl.

I've rewritten your script, but I had to guess at some of your intentions.

I pointedly avoided the use of modules so you could see the logic.

This is not how I would write the code. This is strictly for your learning benefit.

#!/usr/bin/perl use strict; use warnings; # Using this array to simulate the input file (reduces code size) my @Inpbuf = ( 'old/path/to/some/file', 'another/old/path/to/some/file', 'one/more/old/path/to/some/file', 'bad/cold/example/old/path/to/some/file', ); # This is the old path my $Oldpat = 'old'; # This is the desired new path my $Newpat = 'new'; # This is a horrible way to specify the directory separator (reduces c +ode size) my $Dirsep = "/"; my $Dirrgx = quotemeta $Dirsep; # Loop through the input and demonstrate two ways to perform the adjus +tment foreach my $inpbuf (@Inpbuf) { # Translation here so we can support future multiple translation l +ogic my $oldrgx = quotemeta $Oldpat; # This should produce unwanted results on fourth line of data my $tstbuf = $inpbuf; $tstbuf =~ s/$oldrgx/$Newpat/g; print " Risky transation: [$inpbuf] -> [$tstbuf]\n"; # This probably has a higher rate of intended results my @inpelt = split /$Dirrgx/, $inpbuf; my @outelt = (); foreach my $inpelt (@inpelt) { my $outelt = $inpelt; $outelt =~ s/^$oldrgx$/$Newpat/; push @outelt, $outelt; } my $outbuf = join $Dirsep, @outelt; print "Proper translation: [$inpbuf] -> [$outbuf]\n"; print "\n"; } exit; __END__

My results:

P:\>chg10.pl Risky transation: [old/path/to/some/file] -> [new/path/to/some/file +] Proper translation: [old/path/to/some/file] -> [new/path/to/some/file +] Risky transation: [another/old/path/to/some/file] -> [another/new/p +ath/to/some/file] Proper translation: [another/old/path/to/some/file] -> [another/new/p +ath/to/some/file] Risky transation: [one/more/old/path/to/some/file] -> [one/more/new +/path/to/some/file] Proper translation: [one/more/old/path/to/some/file] -> [one/more/new +/path/to/some/file] Risky transation: [bad/cold/example/old/path/to/some/file] -> [bad/ +cnew/example/new/path/to/some/file] Proper translation: [bad/cold/example/old/path/to/some/file] -> [bad/ +cold/example/new/path/to/some/file] P:\>


In reply to Re: Writing to a file by marinersk
in thread Writing to a file by victorz22

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.