in reply to Help with substitution - - 15 Characters from the left

Here's an (almost) one-line solution.

It took a few minutes, but this looks like a good prototype for your needs. You will likely need to adjust the tests on the position variable $i to fit your need more precisely...

use strict; my $str = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n'; print "$str\n"; my $i = 0; $str =~ s/(.)/$i++;($1 ne ',' or $i>15 && $i<25) ? $1 : '#'/ge; print "$str\n";
This prints:
a,b,c,d,e,f,g,h,i,j,k,l,m,n a#b#c#d#e#f#g#h,i,j,k,l,m#n
I used '#' instead of "\t" for demo purposes so things would line up nicely in the print test.

Note: along the way I tried using pos instead of $i, but apparently it is undefined while still in the right side of the s///. Update: Hofmator is correct to say I am wrong to imply that pos would work outside the s///.   pos is not set at all for s///.

Note 2: I offer this as a solution to the problem as you described it but it seems very odd that you would have a list of companies with names that are exactly 10 characters long.

Replies are listed 'Best First'.
Re: Re: Help with substitution - - 15 Characters from the left
by La12 (Sexton) on Aug 14, 2001 at 00:06 UTC
    re: note 2

    The actual is number is 23 characters. I gave 10 in my example 'cause it's a nice round number. True, my sample data does not show this, that's why I said...

    "Now the one thing that is consistent (my example doesn't show this) is that the company names will always start at the 15th character from the left and end at the 25th character from the left..."

    If a company name has more than 23 characters, the name would get cut off (I have no control over the data that is given to me).

    Thank you for your example. I think that it is exactly what I needed. I will also look at pos() as I am unfamiliar with it.
Re: Re: Help with substitution - - 15 Characters from the left
by Hofmator (Curate) on Aug 14, 2001 at 12:06 UTC

    Note: along the way I tried using pos() instead of $i, but apparently it is undefined while still in the right side of the s///
    well, this is right, but it is also undefined outside the s/// construct!! That's because (from perldoc)
    pos returns the offset of where the last "m//g" search left off
    and that's it. Pos is not set with s///.

    -- Hofmator