princepawn has asked for the wisdom of the Perl Monks concerning the following question:

Title says it all. I tried a few things, but they all failed.

While we're at it, how about a hash reference slice?

  • Comment on Can you take a slice of an array reference?

Replies are listed 'Best First'.
(ar0n) Re: Can you take a slice of an array reference?
by ar0n (Priest) on May 24, 2001 at 06:25 UTC
    Array ref:
    $a = [ 1 .. 30 ]; print @{ $a }[0,1,2,5..10,19]; # prints 1236789101120
    Hash ref:
    $h = { foo => 1, bar => 2, baz => 3 }; print @{ $h }{foo, baz}; # prints 13


    ar0n ]

Re: Can you take a slice of an array reference?
by clintp (Curate) on May 24, 2001 at 06:25 UTC
    How about:
    @a=qw(one two three four five six); $r=\@a; print @{$r}[0..2]; # Gives onetwothree
        Because when I'm explaining in class, or in a book, I usually leave the braces around the expression for clarity. Given:
        $a=\@foo;
        that $a is now a reference to an array and, syntatically, it can be used as a stand-in for an array name. Now knowing this, treat @{$a} as you would @foo and get the same results:
        @{$a}; # Same as @foo ${$a}[1]; # Same as $foo[1] @{$a}[0..2]; # Same as @foo[0..2]
        It's just a teaching tool, and I've used it before with great success. And when they're confortable with this you can remove the braces (reminding them of precedence) and when they've got that down pat, show them -> where they get a dereference for free.
Re: Can you take a slice of an array reference?
by Zaxo (Archbishop) on May 24, 2001 at 14:34 UTC
    No, but you can do what you want by other means.

    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

(tye)Re: Can you take a slice of an array reference?
by tye (Sage) on May 24, 2001 at 21:00 UTC

    Whenever I see this question I always think "Well, you must not have read References quick reference." (:

    It is supposed to be in Tutorials, but it appears to still not been approved. :( Anyway, after I realized what I describe there, using references has never again been confusing.

            - tye (but my friends call me "Tye")