String question

Discussion in 'Perl Misc' started by Santana, Sep 7, 2007.

  1. Santana

    Santana Guest

    Hei all,
    i'am a newbie in PERL and i find a solution for this problem :

    I have a string "xxxxxxx" , i want put ones("1") on left of string, if
    this string dont have a length of 20 character.

    Example :

    if i have th string "HELLO" and woul like get this ;

    "111111111111111HELLO"


    How i can this ???



    Thanks,
    Paulito
     
    Santana, Sep 7, 2007
    #1
    1. Advertisements

  2. $ perl -le'
    my $string = "HELLO";

    print substr "1" x 20 . $string, -20;
    '
    111111111111111HELLO



    John
     
    John W. Krahn, Sep 7, 2007
    #2
    1. Advertisements

  3. Santana

    Mirco Wahab Guest

    Just by printing the proper count of '1's in front of the string:

    ...
    $string = 'xxxxxxx';
    ...
    $string = '1' x (20-length $string) . $string;


    No rocket science required here ;-)

    Regards

    M.
     
    Mirco Wahab, Sep 7, 2007
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.