replace everything from the 6th character on with asterisks

J

jaredsubman

The code I have to do this so far is:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;
# join both parts
my $new_string = "$match1$match2";
print "$new_string\n";

But surely, there must be a better way to do this, preferably a one-
liner. Any ideas?
Thanks in advance.
 
J

Jürgen Exner

The code I have to do this so far is:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;
# join both parts
my $new_string = "$match1$match2";
print "$new_string\n";

But surely, there must be a better way to do this, preferably a one-
liner. Any ideas?

Indeed there is. You can either use substr() as an lvalue and assign to
the second half of the string

substr($string, 5) = '*' x (length($string)-5);

or you can use the 4-argument form of substr() directly:

substr($string, 5, length($string)-5, '*' x ((length($string))-5));

jue
 
W

Willem

(e-mail address removed) wrote:
) The code I have to do this so far is:
)
) #!/usr/bin/env perl
)
) use strict;
) use warnings;
)
) my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
) print "$string\n";
) # grab first 5 characters
) my $match1=substr($string,0,5);
) # grab from the 6th character to the end of the string
) my $match2=substr($string,5);
) # replace all of the 2nd set of matched chars with asterisks
) $match2 =~ tr/[0-9][a-z][A-Z]/*/;
) # join both parts
) my $new_string = "$match1$match2";
) print "$new_string\n";
)
) But surely, there must be a better way to do this, preferably a one-
) liner. Any ideas?

To keep most in line with your program:

substr($string, 5) =~ tr/[0-9][a-z][A-Z]/*/;

You see, you can use the result of subsrt as an lvalue.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

John W. Krahn

The code I have to do this so far is:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;
# join both parts
my $new_string = "$match1$match2";
print "$new_string\n";

But surely, there must be a better way to do this, preferably a one-
liner. Any ideas?

$ perl -le'
my $string = q/ABCDEFG8IJK2MNOPbRST7VW5YZ/;
print $string;
substr( $string, 5 ) =~ tr//*/c;
print $string;
'
ABCDEFG8IJK2MNOPbRST7VW5YZ
ABCDE*********************



John
 
S

sln

The code I have to do this so far is:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;
# join both parts
my $new_string = "$match1$match2";
print "$new_string\n";

But surely, there must be a better way to do this, preferably a one-
liner. Any ideas?
Thanks in advance.

Yes, as Willem said, as an lvalue its an alias to a mechanism
that knows how to write to that location. So you can to tr/// on it
or anything you could on a normal variable, within the rules of substr.

substr($string,5) =~ tr/[0-9][a-z][A-Z]/*/;
same as
${\substr($string,5)} =~ tr/[0-9][a-z][A-Z]/*/;

or can get a reference and do it later

$ref = \substr($string,5);
$$ref =~ tr/[0-9][a-z][A-Z]/*/;

-sln
 
J

John W. Krahn

The code I have to do this so far is:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;

Repeated characters will be ignored so that would more simply be:

$match2 =~ tr/[]0-9a-zA-Z/*/;

Although I don't see any '[' or ']' characters in your string.



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top