in reply to Can you take a slice of an array reference?
A reference is a reference. It's value is something like ARRAY(0xhhhhhhh) or CGI=HASH(0xhhhhhhhh), i.e. blessings if any, type and an address. All references are scalars. Their value determines what operations can be done on their referent.
To deal correctly with references, dereference them to the referent's type, and apply the methods of that type. Hint: look at the precedence of dereferencing operators alongside the type operations. Also, it matters whether the referent is anonymous.
Here's some code to play with. If you duplicate the error case code you can see what your experiments do.
The listing:
___
#!/usr/bin/perl -w # -*-Perl-*- use strict; use CGI; my $cgi = new CGI(); # Just for the reference my $hrf = { 'a'=>['alpha','start','soup'], 'b'=>['beta','middle','fish'], 'z'=>['omega','finish','nuts'], }; my $n = "\n"; print "Value of my \$cgi:\t",$cgi,$n,$n; print "Successive dereferences of a reference to a\n"; print "hash of references to arrays:\n"; print "\$hrf:\t",$hrf,$n; print "\$hrf->{'a'}:\t",$hrf->{'a'},$n; print "\$hrf->{'a'}->[1]:\t",$hrf->{'a'}->[1],$n; print "\nTry forcing our \$hrf to be to an array:\n"; print "\@{\$hrf}[2]:\t"; print eval{@{$hrf}[2]} || $@, $n; print "Pole to polar phrases:$n"; for my $idx (0..2) { print join(' to ', map {$hrf->{$_}->[$idx]} sort keys %{$hrf} ),$n +; } print $n; print "Our slice of the menu is:$n"; for ((sort keys %{$hrf})[0,-1]) { print @{$hrf->{$_}}[0, -1],"\n"; }
The output:
___
Value of my $cgi: CGI=HASH(0x80cb854) Successive dereferences of a reference to a hash of references to arrays: $hrf: HASH(0x8173438) $hrf->{'a'}: ARRAY(0x80cb824) $hrf->{'a'}->[1]: start Try forcing our $hrf to be to an array: @{$hrf}[2]: Not an ARRAY reference at ./refs line 22. Pole to polar phrases: alpha to beta to omega start to middle to finish soup to fish to nuts Our slice of the menu is: alphasoup omeganuts
After Compline,
Zaxo
|
|---|