2-dimensional array into 1 dimensional array?
Author |
Message |
Bo #1 / 6
|
 2-dimensional array into 1 dimensional array?
I have a 2-dimensional array (array of arrays) that I want take the contents of one "row" of the array and store in another 1 dimensional array. As follows...
['3rd', '1st', 'b'], ['3rd', '2nd', 'c'], ['2nd', '1st', 'a'], ['1st', '1st', 'b'], ['1st', '1st', 'a']);
# Display the new array. print $zork[0], "\n"; print $zork[1], "\n"; print $zork[2], "\n"; The "print" command returns nothing recognizable/usable. This looks like it should be easy, and I'm probably missing something obvious. Thanks in advance. Bob
|
Sat, 29 Nov 2003 02:50:55 GMT |
|
 |
Anno Sieg #2 / 6
|
 2-dimensional array into 1 dimensional array?
Quote: > I have a 2-dimensional array (array of arrays) that I want take the > contents of one "row" of the array and store in another 1 dimensional > array. As follows... > #create the 2 dimensional array
> ['3rd', '1st', 'b'], > ['3rd', '2nd', 'c'], > ['2nd', '1st', 'a'], > ['1st', '1st', 'b'], > ['1st', '1st', 'a']); > # (Attempt to) grab the contents of "row" 2.
You are missing a step of dereferencing here. $test_array[2], being an array element, can only hold a scalar value, namely a reference to
an array in front:
of your code should now work as expected. Quote: > # Display the new array. > print $zork[0], "\n"; > print $zork[1], "\n"; > print $zork[2], "\n"; > The "print" command returns nothing recognizable/usable.
It printed the stringified version of an array ref. While it may indeed be not very useful, the pattern is worth memorizing. When you see it, you may assume that you have treated an arrayref as a string, which is valuable debugging information. Anno
|
Sat, 29 Nov 2003 03:15:57 GMT |
|
 |
Andras Malatinszk #3 / 6
|
 2-dimensional array into 1 dimensional array?
Quote:
> I have a 2-dimensional array (array of arrays) that I want take the > contents of one "row" of the array and store in another 1 dimensional > array. As follows... > #create the 2 dimensional array
> ['3rd', '1st', 'b'], > ['3rd', '2nd', 'c'], > ['2nd', '1st', 'a'], > ['1st', '1st', 'b'], > ['1st', '1st', 'a']); > # (Attempt to) grab the contents of "row" 2.
side, you should get suspicious. What you should keep in mind is that Perl arrays can only hold scalars.
array of array references. $test_array[2] is a reference to ['3rd', '2nd', 'c'], which probably looks something like ARRAY(0xba58e8). You should not think that it is "nothing recognizable/usable," though, because you can dereference that reference to get back the original array it refers to. What you need to do is this:
|
Sat, 29 Nov 2003 03:15:07 GMT |
|
 |
Philip Newto #4 / 6
|
 2-dimensional array into 1 dimensional array?
Quote: > # (Attempt to) grab the contents of "row" 2.
$test_array[2] is a reference to an array, not an array. To assign it to an array, you need to dereference it:
Quote: > This looks like it should be easy, and I'm probably missing something > obvious.
Suggested reading: perldsc, perlreftut, perllol. Cheers, Philip --
That really is my address; no need to remove anything to reply. If you're not part of the solution, you're part of the precipitate.
|
Sat, 29 Nov 2003 04:07:52 GMT |
|
 |
Bo #5 / 6
|
 2-dimensional array into 1 dimensional array?
That did the trick. Thanks to all who responded. :-) Bob Quote:
> > # (Attempt to) grab the contents of "row" 2.
> $test_array[2] is a reference to an array, not an array. To assign it to > an array, you need to dereference it:
> > This looks like it should be easy, and I'm probably missing something > > obvious. > Suggested reading: perldsc, perlreftut, perllol. > Cheers, > Philip
|
Sat, 29 Nov 2003 23:49:32 GMT |
|
 |
nob.. #6 / 6
|
 2-dimensional array into 1 dimensional array?
Quote:
> > ['3rd', '1st', 'b'], > > ['3rd', '2nd', 'c'], > > ['2nd', '1st', 'a'], > > ['1st', '1st', 'b'], > > ['1st', '1st', 'a']);
Typo!
-- \\ ( ) . _\\__[oo
. l___\\ # ll l\\ ###LL LL\\
|
Sun, 30 Nov 2003 00:42:42 GMT |
|
|
|