replace everything from the 6th character on with asterisks

Discussion in 'Perl Misc' started by jaredsubman@yahoo.com, Aug 19, 2009.

  1. Guest

    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.
     
    , Aug 19, 2009
    #1
    1. Advertising

  2. "" <> 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
     
    Jürgen Exner, Aug 19, 2009
    #2
    1. Advertising

  3. Willem Guest

    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
     
    Willem, Aug 19, 2009
    #3
  4. 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
     
    John W. Krahn, Aug 19, 2009
    #4
  5. Guest

    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
     
    , Aug 19, 2009
    #5
  6. 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
     
    John W. Krahn, Aug 19, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard B. Katz
    Replies:
    0
    Views:
    1,221
    Richard B. Katz
    Jun 27, 2003
  2. Dan Sikorsky
    Replies:
    1
    Views:
    529
    Teemu Keiski
    Apr 7, 2005
  3. Stephen Boulet
    Replies:
    7
    Views:
    358
  4. AMT2K5
    Replies:
    1
    Views:
    514
    Jim Langston
    Nov 17, 2005
  5. herman
    Replies:
    5
    Views:
    7,636
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
Loading...

Share This Page