2

For eg, I want to store the contents of the command ipconfig in an array, such that each line of output is stored in a new index of array, i.e. array[0] contains the 1st line output. array[1] contains the 2nd line output and so on.

How can I achieve that?

3
  • I would assign the captured output using backticks into a scalar, and use spilt and reassign it to an array. That's one way to go about it. Commented Sep 1, 2016 at 6:42
  • No, no, no @Ghost. Backticks in list context already captures the output into an array, one $/-ending line per element. Commented Sep 1, 2016 at 13:44
  • @mob: Oh! I wasn't aware of that. Thanks for letting me know about this. Will keep this in mind going further Commented Sep 2, 2016 at 9:39

1 Answer 1

9

A simple

@array = `ipconfig`;

does the trick. Note that, as Borodin rightly points out, the array elements so assigned include the newlines.

Sign up to request clarification or add additional context in comments.

2 Comments

@Coder: Remember that each line in the array will be terminated by a newline character which, depending on your application, may need to be chomped.
Got it! Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.