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?

Jens
  • 69,818
  • 15
  • 125
  • 179
Coder
  • 65
  • 1
  • 2
  • 8
  • 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. – Ghost Sep 01 '16 at 06:42
  • No, no, no @Ghost. Backticks in list context already captures the output into an array, one `$/`-ending line per element. – mob Sep 01 '16 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 – Ghost Sep 02 '16 at 09:39

1 Answers1

9

A simple

@array = `ipconfig`;

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

Jens
  • 69,818
  • 15
  • 125
  • 179