1

This is my source array:

my @raw_stack = (
    'a1~a2~a3~a4~a5',
    'b1~b2~b3~b4~b5',
    'c1~c2~c3~c4~c5',
    'd1~d2~d3~d4~d5',
    'e1~e2~e3~e4~e5',
);

I want get the 3rd value in '~' pattern then place that to another array.

The other array should now look like this:

my @other_stack = (
    'a3',
    'b3',
    'c3',
    'd3',
    'e3',
);

I could go about looping through the stack array then split
and push to another array, but i'm looking for a lean way
to code this.

Any ideas?

2 Answers 2

3
my @other_stack = map {(split/~/)[2]} @raw_stack;
Sign up to request clarification or add additional context in comments.

Comments

2

Use map to list transform instead of push. Use index/substr or unpack if the items are fixed-width. This generally is faster than split, which uses regex.

3 Comments

It is misguided to pursue efficiency before an obvious and clear solution has proven to be inadequate. Always go for readability and elegance before anything else.
If the OP asks an optimisation question, I take it on in good faith. How come you want to blame me?
Ah I see. I didn't consider that lean may mean fast. I think it more likely that he wanted something more concise that the for / split / push approach that he imagined. I guess we'll never know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.