How do I build a 2d matrix using STDIN?
If I input a matrix like so:
1 2 3
4 5 6
7 5 6
7 8 9
4 5 6
3 3 3
how do I input this and create two matrices out of this?
Here’s my code so far
while (defined ($a=<STDIN>)) {
chomp ($a);
push @a,($a);
}
This is just for the input.
My understanding is I can just add each row to a stack. When the matrices are all put in I can take each line, break by space to create an array. I then need to create an array reference and push this reference into an array to create my matrix. How the heck do I do this? Is there an easier way? I’ve been bashing my head on this for 3 days now. I feel pretty damn stupid right now…
The other answers seem to be missing the requirement to read multiple matrices from the same input, breaking on a blank line. There are a few different ways to go about this, including frobbing
$/, but here’s one that appeals to me.Season the last part to taste, of course — you might be using an explicitly opened handle instead of ARGV magic, and you might know in advance how many things you’re reading instead of going to EOF, etc.