> > On Friday, June 25, 2004, at 11:33 AM, Wiggins d Anconia wrote: > > While you can dereference the array reference like you are doing, Perl > > allows a simpler syntax for readability when accessing into a > > reference, > > specifically the little arrow operator. So the above test can be > > written more simply as, > > > > if ($foo1->[0] eq 'yes') { > > Thank you! I'm using a very dated version of a Perl handbook which > made no reference (no pun intended) to that at all! > > > > >> my @[EMAIL PROTECTED]; > > > > Right here I *believe* you are slicing into the dereferenced top level > > array, and returning the array reference in index 1 into the first > > element of @ar, but what you really want to be doing is dereferencing > > the array in index 1 into @ar, again the arrow operator can simplify > > this, though you will need to add a set of braces as well. > > Yes, that was a slice. I forgot to fix the print statement, but the > following if() had the correct use of $$ vs. @$. Whoops! > > > my @ar = @{ $foo1->[1] }; > > So, to be sure I am reading this correctly: {$foo1->[1]} dereferences > the value at position 1 of $foo1, and the preceding @ treats the > dereference as an array?
Well close, I think this is a semantics issue rather than an understanding one, so I wouldn't worry much since writing the code is probably more important first than being able to talk about it. $foo1->[1] is not really a dereference, it is just accessing another reference from an array reference, in other words if you were to then act on that value you would be acting on the reference. The dereference is really where you turn that reference back into the original/base/etc. type of structure, which is really what the @{ } is doing. The only reason the {} are needed at all is to disambiguate the reference that we want to dereference, aka $foo1->[1] instead of just $foo1. Does this mean that the following line: > > if ($foo1->[0] eq 'yes') { > > could also have been > > if (${$foo1->[0]} eq "yes") { > > ..? If you do not specify a variable type, does it default to a string? No (and yes). The ${$foo1->[0]} would be dereferencing a scalar reference (and in the case that you have strictures turned on would cause an error). This should help as an example of what I said before where $foo1->[0] just *accesses* a value, be it a plain string or a reference (to an array, hash, scalar, sub, object, etc.). But it is the ${ } that causes the dereference, and since $foo1->[0] didn't (in this case) hold a reference there can't be a derefence. The reason why I said (and yes) is because Perl has soft references as well, but you should avoid them as much as possible, and certainly until you understand the difference. > > > if you see ARRAY(0x...) or HASH(0x...) that indicates a reference to > > that type of data structure, that you will then need to dereference. > > Yes, this I figured out. I was experimenting earlier and found that > $foo2[0][0] would work, but not $foo2[0]. I just couldn't get past the > "first" dereference. This kind of confusion is one of the reasons why I don't like the shorthand format, aka dropping the little arrow. IMHO, it is easier to read it as $foo2->[0]->[0], it reinforces that what $foo2->[0] holds is an array reference, and that $foo2 is an array reference itself. > > > Close, and keep at it. For some bedtime reading that will help with > > all > > of this you should check out, > > > > perldoc perldsc > > perldoc perllol > > perldoc perlreftut > > perldoc perlref > > > > Once you "get" references a whole other world will open up, good > > luck,.... > > Yes.. A lot of my stuff is hardcoded right now, but using lists of > lists makes things a lot easier (especially since sublists do not have > to be the same size! Thank you Perl!). I'll check ou the perldoc's you > listed. > > Cheers, > Patrick > > HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>