#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Iterating over same thread


    Hi perl guru's,

    I have a small problem here.. May be very simple for you guys.

    I need to iterate over a same thread till it reaches some count. How do I get to do it? FYI, I am implementing Vigenere Cipher in PERL.

    Example, the message is : MEETMEFORLUNCH
    the keyword is : KING

    So, I have to repeat KING for the entire length of the message, so eventually my keyword would be KINGKINGKINGKI

    How do i do this ??
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    I'm sure there are more elegant ways, but this was the first thing that came to my mind:

    Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    
    my $lk = length $keyword;
    my $ls = length $string;
    
    my $num = int($ls / $lk) + 1;
    my $tmpstr = $keyword x $num;
    
    my $keystring = substr $tmpstr, 0, $ls;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Or possibly this way (untested, just the general idea):

    Perl Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    my str_length = length $string ;
    my $key =. $keyword while length $key < str_length;
    $key = substr $key, 0, str_length;


    But that's OK only if the string is not much much longer than $keyword. If the difference is large, something like this:

    Perl Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    my $str_length = length $string ;
    my $key = $keyword x ( $str_length / length $key + 1);
    $key = substr $key, 0, str_length;


    Which is basically similar to the solution proposed by Keath, just a tiny bit shorter.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Wow... thanks ... i wrote a division operation, a modulo operation and then a while loop for this simple stuff... this is awesome...


    Originally Posted by keath
    I'm sure there are more elegant ways, but this was the first thing that came to my mind:

    Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    
    my $lk = length $keyword;
    my $ls = length $string;
    
    my $num = int($ls / $lk) + 1;
    my $tmpstr = $keyword x $num;
    
    my $keystring = substr $tmpstr, 0, $ls;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Thanks guys.. short n simple...

    Originally Posted by Laurent_R
    Or possibly this way (untested, just the general idea):

    Perl Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    my str_length = length $string ;
    my $key =. $keyword while length $key < str_length;
    $key = substr $key, 0, str_length;


    But that's OK only if the string is not much much longer than $keyword. If the difference is large, something like this:

    Perl Code:
    my $keyword = 'KING';
    my $string = 'MEETMEFORLUNCH';
    my $str_length = length $string ;
    my $key = $keyword x ( $str_length / length $key + 1);
    $key = substr $key, 0, str_length;


    Which is basically similar to the solution proposed by Keath, just a tiny bit shorter.

IMN logo majestic logo threadwatch logo seochat tools logo