Re: Push to AoA
On Wed May 13 2009 @ 9:22, Steve Bertrand wrote: Jim Gibson wrote: The first argument of push should be an array, not a scalar (even if that scalar is a reference to an array). push( @{$graph_data-[0]}, $x_axis ); I'll have to do a bit of reading, because I can't remember why the braces are important here. All I know is that it works ;) The general rule for using a reference is write what you would have written, but put the reference inside of brackets. So, for example, Regular Reference @array@{$array_ref} %hash %{$hash_ref} If the reference itself is a simple scalar variable, then the brackets aren't required. So both of the cases above could be written more simply as @$array_ref and %$hash_ref. However, if the reference is not a simple scalar variable, then the brackets are required, not optional. In your case, the variable $graph_data-[0] is itself a reference, so the brackets aren't optional. The other thing that may be confusing you is that if you are referring to a single item from a hash or array, you can use the arrow syntax rather than the bracket syntax. So, deciding what you need is a two stage process: (1) Am I trying to use an entire hash or array reference? (a) Yes: wrap the reference inside of { } and put the proper sigil on it. Eg, @{$array_ref} %{$hash_ref} (b) Yes and the reference itself is a simple scalar: ok, then the braces are optional. Eg, @$array_ref, %hash_ref (2) Am I trying to use a scalar from an array or hash? (a) Yes: Instead of ${$ref} use $ref- Some people say always use the brackets for whole arrays or hashes (ie, 1a rather than 1b) simply because fewer choices are fewer things to remember or mess up. Others hate the look of @{$array_ref} so much that they say use 1b whenever possible. This may help: http://www.perlmonks.org/?node_id=69927 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Push to AoA
On 5/13/09 Wed May 13, 2009 4:48 PM, Steve Bertrand st...@ibctech.ca scribbled: Hi all, I'm trying to push a scalar onto an array. The array I'm trying to push() to is the first element of another array. I can't figure out what I'm missing. It would be appreciated if someone could point me in the right direction. if (-e $data_file) { $graph_data = retrieve $data_file; $x_axis = $graph_data-[0][0]; $x_axis++; # start pushing the next days data into the aoa push ($graph_data-[0], ($x_axis)); store $graph_data, $data_file; } Your program shouldn't even compile. I get: Type of arg 1 to push must be array (not array element) at ... The first argument of push should be an array, not a scalar (even if that scalar is a reference to an array). push( @{$graph_data-[0]}, $x_axis ); (the parentheses around $x_axis are unnecessary.) If you are having more trouble, use the Data::Dumper module to inspect the contents of your AoA: use Data::Dumper; print Dumper(\$graph_data); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Push to AoA
Jim Gibson wrote: On 5/13/09 Wed May 13, 2009 4:48 PM, Steve Bertrand st...@ibctech.ca scribbled: Hi all, I'm trying to push a scalar onto an array. The array I'm trying to push() to is the first element of another array. I can't figure out what I'm missing. It would be appreciated if someone could point me in the right direction. if (-e $data_file) { $graph_data = retrieve $data_file; $x_axis = $graph_data-[0][0]; $x_axis++; # start pushing the next days data into the aoa push ($graph_data-[0], ($x_axis)); store $graph_data, $data_file; } Your program shouldn't even compile. I get: Type of arg 1 to push must be array (not array element) at ... That is where I was at as well. I just wanted to provide a code snippet that provided proper context. The first argument of push should be an array, not a scalar (even if that scalar is a reference to an array). push( @{$graph_data-[0]}, $x_axis ); I'll have to do a bit of reading, because I can't remember why the braces are important here. All I know is that it works ;) If you are having more trouble, use the Data::Dumper module to inspect the contents of your AoA: I was using Dumper, but I was getting results that I didn't expect, and I didn't know why. However, with your help, things are looking good. I'm now on track to start pushing onto all of the arrays: pearl# ./simscan-stats.pl yesterday ***5 $VAR1 = [ [ 1, '2', '3', '4', '5' ], [ 854 ], [ 388 ], [ 287 ], [ 511 ], [ 146 ], [ 365 ] ]; Thanks! Steve smime.p7s Description: S/MIME Cryptographic Signature