Hello,
Today I have an array, and I want to reverse and push it, to make a final array become palindromic.
Upd.If initial array has even length, I want to double the length, otherwise I want doubled and -1 length. I've tried this expression:
push @array, reverse @array[ 0 .. $n / 2 - 1 ];
Where $n - is a length of final array (wanted length).
For some test cases:
@arr =
qw(A B C D E)
qw(A B C D)
qw(A B C)
qw(A B)
qw(A)
It outputs:
A B C D E D C B A
A B C D D C B A
A B C B A
A B B A
A A
I would like to get only one 'A' (n = 1) in the last output line. Intuitively it looked like '0 .. 1/2 - 1' is an empty list, however '0 .. -0.5' is being converted to '0 .. 0', so it is a non-empty range, because '..' asks its operands to become integers, i.e. 'int(-0.5)' becomes '0' (opposing to POSIX:floor).
So even ranges '0 .. -0.5' and '0 .. 0.5' are equal.
What expression would you suggest to put into brackets to achieve my goal:
push @array, reverse @array[ EXPR ];
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.