I'm sure this is simple, but I can't seem to align my thoughts right.
I have a file that I'm reading into an array with a series of values. For the sake of arguement we'll say there's three numbers, then any number of other numbers.
I want to store the first three numbers separately, and then the rest as an array.
[tt]
my ($s1, $s2, $s3, @a) = split(/\s/, $_);
[/tt]
works quite happily within a while (<F>) loop.
the next line sticks that stuff into an array of hashes (each index of the array represents the line number essentially)
[tt]
push (@lines, {sc1 => $s1, sc2 => $s2, sc3 => $s3, arr => \@a});
[/tt]
and also seems to work quite fine. all the data can be accessed easily through
[tt]
$lines[idx]{sc1}
$lines[idx]{sc2}
$lines[idx]{sc3}
$lines[idx]{arr}[0] to $lines[idx]{arr}[n]
[/tt]
Ok, all that works fine. Here's the part I'm stuck at.
Occasionally I want to shift one of those array elements off. Through experimentation, it seems that shift returns a reference to the array element. right?
[tt]
$lineref = shift(@lines);
[/tt]
now I want information added into a NEW array of hashes. The only way I can think to do this is something like this:
[tt]
my %l2;
$l2{sc1} = ${%$lineref}{sc1};
$l2{sc2} = ${%$lineref}{sc2};
$l2{sc3} = ${%$lineref}{sc3};
$l2{arr} = ${%$lineref}{arr};
[/tt]
followed by
[tt]
push(@newarray, \%l2)
[/tt]
However, that's where my problem lies. the scalars move into the new array right, but ${%$lineref}{arr} doesn't. I also discovered that by that point, simply to access one of the values of that array, I have to dereference 3 times. For example, the first element at that point is:
${@{${%$lineref}{arr}}}[0]
Anyway, that might be irrelevant. My question is this. How do I transfer the array stored in ${%$lineref}{arr} to the new array, such that it can be accessed like this:
$newarray[idx]{arr}[0] through $newarray[idx]{arr}[n]
It really shouldn't be that complicated. I'm sure I'm just overlooking something.
Thanks
I have a file that I'm reading into an array with a series of values. For the sake of arguement we'll say there's three numbers, then any number of other numbers.
I want to store the first three numbers separately, and then the rest as an array.
[tt]
my ($s1, $s2, $s3, @a) = split(/\s/, $_);
[/tt]
works quite happily within a while (<F>) loop.
the next line sticks that stuff into an array of hashes (each index of the array represents the line number essentially)
[tt]
push (@lines, {sc1 => $s1, sc2 => $s2, sc3 => $s3, arr => \@a});
[/tt]
and also seems to work quite fine. all the data can be accessed easily through
[tt]
$lines[idx]{sc1}
$lines[idx]{sc2}
$lines[idx]{sc3}
$lines[idx]{arr}[0] to $lines[idx]{arr}[n]
[/tt]
Ok, all that works fine. Here's the part I'm stuck at.
Occasionally I want to shift one of those array elements off. Through experimentation, it seems that shift returns a reference to the array element. right?
[tt]
$lineref = shift(@lines);
[/tt]
now I want information added into a NEW array of hashes. The only way I can think to do this is something like this:
[tt]
my %l2;
$l2{sc1} = ${%$lineref}{sc1};
$l2{sc2} = ${%$lineref}{sc2};
$l2{sc3} = ${%$lineref}{sc3};
$l2{arr} = ${%$lineref}{arr};
[/tt]
followed by
[tt]
push(@newarray, \%l2)
[/tt]
However, that's where my problem lies. the scalars move into the new array right, but ${%$lineref}{arr} doesn't. I also discovered that by that point, simply to access one of the values of that array, I have to dereference 3 times. For example, the first element at that point is:
${@{${%$lineref}{arr}}}[0]
Anyway, that might be irrelevant. My question is this. How do I transfer the array stored in ${%$lineref}{arr} to the new array, such that it can be accessed like this:
$newarray[idx]{arr}[0] through $newarray[idx]{arr}[n]
It really shouldn't be that complicated. I'm sure I'm just overlooking something.
Thanks