lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dirk Laurie <dirk.laurie@gmail.com> [13-05-18 21:04]:
> 2013/5/18  <meino.cramer@gmx.de>:
> 
> > suppose one have this string of letters:
> >
> >     abcdefghijklmnopqstuvxyz
> >
> > in an array (table) and want to perform these steps:
> 
> I'll do two.
> 
> >     1. shift the string by a certain amount of characters cyclic
> >
> >     abcderfghijklmnopqstuvxyz => ghijklmnopqstuvxyzabcderf
> 
> function string.shift(str,toleft)
>    return str:sub(toleft+1)..str.sub(1,toleft)
> end
> 
> > print(("abcderfghijklmnopqstuvxyz"):shift(7))
> ghijklmnopqstuvxyz
> 
> 
> >     2. take off a character at a certain position
> >
> >     ghijklmnopqstuvxyzabcderf +> g ijklmnopqstuvxyzabcderf
> 
> function string.replace(str,pos,byte)
>    return str:sub(1,pos-1)..byte..str:sub(pos+1)
> end
> 
> You get the idea, I hope.
> 

Hi Dirk,

thank you for info! :)

What I want to achieve is a cyclic shift

    abcderfghijklmnopqstuvxyz => ghijklmnopqstuvxyzabcderf

so that the characters, which gets popped off left will be appended on
the right of the string.

>From my (currently stil a little limited) understanding of lua your
function will exactly do this...:


    return str:sub(toleft+1)..str.sub(1,toleft)
           "shift-part"        "append-aprt"

...but when I execute this it seems that only the "shift-part" gets
executed and the result is 

     ghijklmnopqstuvxyz

instead of

     ghijklmnopqstuvxyzabcdef

Why is the second part, the "append-part", of the function without
any effect to the result...

Thank you very much in advance for any help!

Best regards,
mcc