I need to circular shift some characters/bit using Perl
For example, with the following input (reversing character strings):
1010
1110
hello
I need to obtain:
0101
0111
olleh
How do I achieve this using Perl?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
However, if you really want to circular shift values, it’s easy to write a function that does it with arbitrary lists for training:
A few words of explanation for the
returnline ofcircular:We’re returning an array slice of
@valueshere, which means we evaluate it with a list of indices and return the list of values.We’re returning a list of values here that we calculate from all indices of @values, 0 is the first,
$#valuesis the last.This is what we do with these indices. The
+sign is only used to tell perl that the parantheses are not the argument paratheses formap. We add $i to the index and to prevent that it becomes> $#valueswe modulo divide it by the length of@values. Note that it’s not important to calllength @valueshere because%evaluates its second operand in scalar context and arrays in scalar context evaluate to their length.I leave it up to you as a simple exercise to convert your things like
helloor1110to lists and back to scalars.