Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace everything from the 6th character on with asterisks

Reply
Thread Tools

replace everything from the 6th character on with asterisks

 
 
jaredsubman@yahoo.com
Guest
Posts: n/a
 
      08-19-2009
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.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-19-2009
"" <> 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?


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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      08-19-2009
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-19-2009
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?


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



John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-19-2009
On Wed, 19 Aug 2009 08:03:31 -0700 (PDT), "" <> 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?
>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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-19-2009
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]/*/;


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
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sh conf to display pre-shared keys instead of asterisks networksecurity Cisco 2 03-23-2006 05:59 PM
[C++] Drawing a window border with asterisks AMT2K5 C++ 1 11-17-2005 03:12 AM
Asterisks setting up RH UK VOIP 3 06-30-2005 08:16 AM
HOWTO: Show Asterisks In Password Textboxes During Postbackx Dan Sikorsky ASP .Net 1 04-07-2005 06:27 PM
Single and double asterisks preceding variables in function arguments Stephen Boulet Python 7 01-27-2004 06:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57