Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

a couple of days ago i asked tou guys here for help and you delivered like gods so here i am again with another problem so pls help

Write a script which will do a simple scrambling of a string. The script should have variables for the original string (eg $message) and a number (eg $number) (which should be less than the length of the string). The scrambling should be achieved by reversing the first and last $number characters of $string (in that order). To avoid giving any hints as to the values used the scrambled string should appear all in lowercase.

For example:

String=Babraham Number=2, Scrambed= abbrahma String=Institute Number=3, Scrambled=snititetu

Hint: use the substr() and reverse() functions.

Replies are listed 'Best First'.
Re: Scrambling help
by ww (Archbishop) on Jan 25, 2015 at 21:23 UTC

    Did you fail to mark this as Homework because it's so obvous obvious?

    See On asking for help and How do I post a question effectively?.... and then at least skim through PerlMonks FAQ.

    As LanX noted above, some Monks seem to get some sort of gratification from doing your homework for you, but that's not apt to actually help you learn. And your careless spelling, grammar and (lack-of-) markup hardly reflect any real respect for the quite ordinary human beings who've responded in the past.

    Update: 1.  s/obvous/obvious Thanks LanX! A particularly egregious typo when i/I/me was complaining about careless spelling. 2. Likewise thanks to Not_a_Number re the "s" in some -- it just didn't have strike>anough 3. s/anough/enough/ twists and turns. Why do I think I should have chosen something other than spelling to complain about? But let's stop here, before I commit another typo.

Re: Scrambling help
by LanX (Saint) on Jan 25, 2015 at 20:55 UTC
    what did you try?

    why do you ignore the formating guidelines, especially <p> and <code> ?

    > and you delivered like gods

    unfortunately some monks here love doing the homework of others, it's like feeding stray cats.

    Cheers Rolf

    PS: Je suis Charlie!

      Sorry for the layout. I did manage to do it by "slicem'up" and made the word into 3 different scalars and reversed the first and last, but then i read that i needed to have only 2 variables so now im stuck, iv never used substr and reverse before, i did manage to delete the first 3 and the last 3 letters, but deleted the code in frustration.
        > but deleted the code in frustration.

        Too bad, people here are eager to help improving code.

        Maybe you should try again?

        Cheers Rolf

        PS: Je suis Charlie!

Re: Scrambling help
by ohcamacj (Beadle) on Jan 27, 2015 at 20:49 UTC

    Using substr and reverse ?

    I read that, as a puzzle to solve it without substr and reverse.

    Enjoy.

    sub scramble { (my $str, my $toshuf) = @_; my $len = length($str); if($toshuf * 2 > $len){ return "!error-rorre!"; } $str = lc($str); my @idx = (); my $i = $toshuf; push(@idx, $i--) while $i; push(@idx, ($toshuf + 1) .. ($len - $toshuf)); push(@idx, $len - $i++) while $i < $toshuf; return sprintf(join("", map({"%$_\$s"} @idx)), split(//, $str)); }

      Come on! That smells like C. Not enough line noise, IMO! We obviously need regex :)
      use strict; use warnings; sub scramble { lc $_[0] =~ s{(^.{$_[1]}|.{$_[1]}$)} {join '', (split '', $1)[map -$_, 1..$_[1]]}reg; } print scramble( "Institute", 3 ), "\n"; print scramble( "Babraham", 2 ), "\n";
Re: Scrambling help
by wee (Scribe) on Jan 29, 2015 at 22:08 UTC
    How do you suppose you'll learn anything if you get other people to do your homework for you? I mean, it's not apparent they you even tried to work it out yourself...
Re: Scrambling help
by locked_user sundialsvc4 (Abbot) on Jan 26, 2015 at 15:43 UTC

    This is a homework problem ... obviously ... that will involve (e.g.) the use of the Perl functions splice and join.   Take a good look at the two pages I’ve hyperlinked, and I’ll bet that you will be able to solve this assignment for yourself in a very little while.

    And if you can’t, then your instructor needs to know, and to be the one to help you, so that s/he will also be aware of the points that might need further clarification in an upcoming classroom session.   (If you are having this problem, then you are not the only one of your classmates to be doing so.)   Put the instructor into “the loop.”

      A better solution would use the string functions substr and reverse rather than the array functions you suggest.
      Bill