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

My web host is stuck on Perl 5.12.2 Many of the examples in the 5.24 perldsc just do not work. Like: 5.24 docs sez $thirdList{$itemArea}[$itemId] = [a,b,c] will work, but it doesn't. The push job does though. Need some old geezers to explain "how it used to be done". I'm creating a hash of an array of an array.
use strict; use warnings; my $itemArea; my $itemPrice; my $itemId; my %thirdList; my $area; my $i; $itemArea = "0188"; $itemId = "0000007777"; $itemPrice = "40"; push @{ $thirdList{$itemArea} }, [ $itemArea, $itemId, $itemPrice ]; $itemArea = "0188"; $itemId = "0000009980"; $itemPrice = "41"; push @{ $thirdList{$itemArea} }, [ $itemArea, $itemId, $itemPrice ]; $itemArea = "0123"; $itemId = "0000008888"; $itemPrice = "50"; push @{ $thirdList{$itemArea} }, [ $itemArea, $itemId, $itemPrice ]; $itemArea = "0123"; $itemId = "0000009999"; $itemPrice = "51"; push @{ $thirdList{$itemArea} }, [ $itemArea, $itemId, $itemPrice ]; hardCodedPrint(%thirdList); sub hardCodedPrint { foreach $area ( keys %thirdList) { for ( $i=0; $i <= 1; $i++ ) { print "THIRD: "; print $thirdList{$area}[$i][0], " - "; print $thirdList{$area}[$i][1], " - "; print $thirdList{$area}[$i][2], "\n"; } } }
hardCodedPrint() needs help. What I need is something like for (  $i=0; $i <= $# $thirdList{$area}; $i++  )  { But no matter what kind of casts and !@#$%^&* stuff I put in there (like $# @{ $thirdList{$area} }, I get a "$# is no longer supported" type of error - or worse. Any ideas? Thanks - Dave

Replies are listed 'Best First'.
Re: Perl 5.12.2 Data Structures
by stevieb (Canon) on Sep 22, 2016 at 23:53 UTC

    First, if you want to use the term 'geezers', I recommend you don't use nonsense text speak like 'sez'. Us 'geezers' prefer real language :P

    Next, on 5.10, your code apparently works for me, without knowing what you're expecting... it runs without error:

    This is perl, v5.10.1 (*) built for x86_64-linux
    $ perl x.pl THIRD: 0188 - 0000007777 - 40 THIRD: 0188 - 0000009980 - 41 THIRD: 0123 - 0000008888 - 50 THIRD: 0123 - 0000009999 - 51

    Do you have code that doesn't work? What error do you get when you run it? Perhaps it's due to your example of [a, b, c]. Those are unquoted values you seem to be inserting into an array reference. That won't, and has never worked. Perhaps you meant [qw(a b c)] or [('a', 'b', 'c')], like this (still on 5.10):

    use strict; use warnings; use Data::Dumper; my %h; my $itemArea = 1; my $itemID = 1; $h{$itemArea}{$itemID} = [qw(a b c)]; print Dumper \%h;

    Output:

    $VAR1 = { '1' => { '1' => [ 'a', 'b', 'c' ] } };

    If the 5.24.x docs literally state: $something = [a,b,c], please let us know where, and I'll patch it.

    Also, post your *exact* error, along with the *exact* code that triggers it.

      FWIW,  [ a, b, c ] could also compile to an expression if subroutines  a() b() c() had been defined, but somehow I don't think that's what hennesse was going for.

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub a { return 'foo'; } sub b { } sub c { return 'bar'; } ;; my $array_ref = [ a, b, c ]; dd $array_ref; " ["foo", "bar"]
      (Note that  b() intentionally returns an empty list.)


      Give a man a fish:  <%-{-{-{-<

Re: Perl 5.12.2 Data Structures
by AnomalousMonk (Archbishop) on Sep 23, 2016 at 05:37 UTC

    For your local installation (if it's more or less standard), invoking
        perldoc perldsc
    (or any other documentation module) from your friendly, local command line should get you the doc for your installed version. For the on-line documentation, there's a  Perl version -> Select drop-down in the top left corner of the page. Available versions go all the way back to 5.8.8.


    Give a man a fish:  <%-{-{-{-<

Re: Perl 5.12.2 Data Structures
by tybalt89 (Monsignor) on Sep 22, 2016 at 23:56 UTC
Re: Perl 5.12.2 Data Structures
by Anonymous Monk on Sep 23, 2016 at 00:17 UTC
    But no matter what kind of casts and !@#$%^&* stuff I put in there (like $# @{ $thirdList{$area} }, I get a "$# is no longer supported" type of error

    Sometimes, you need squigglies.

    $#{$thirdList{$area}}

    Alternately,

    while (my ($area, $list) = each %thirdList) { foreach my $item (@$list) { print join(' - ', @$item), "\n"; } }
Re: Perl 5.12.2 Data Structures
by hennesse (Beadle) on Sep 23, 2016 at 01:22 UTC

    Thanks guys.

    Mr. Anonymous - your squigglies worked like a champ, although I would swear I already tried them - and every other darn key on the keyboard too.

    Stevieb - I'm sorry - precise language is important, but sometimes us "hep cats" who cut our teeth on GE 235*s with paper tape on a Teletype sometimes regress to the "flash cant" of aeons past.

    I have had problems getting the knack of some of these complex data structures because there seems to have been a lot of changes since 5.12, and I could never get things to work reliably on different systems.

    In case you're wondering, I'm working on a big improvement to the background search for: http://www.harleykmodel.com/craigslist

    Dave

    * http://archive.computerhistory.org/resources/text/GE/GE.235.1964.102646091.pdf

      Hi hennesse,

      Many of the examples in the 5.24 perldsc just do not work.

      ... there seems to have been a lot of changes since 5.12

      There have been a few changes since v5.12 (for example, the addition of the Postfix Dereference Syntax and Key/Value Hash Slices, and for a while there was the experimental and now-removed "autoderef" feature), but as far as I can remember they've all been backwards compatible (and perldsc hasn't had any significant changes in the code examples for 15 years). Could you point out the examples in perldsc that don't work for you?

      Thanks,
      -- Hauke D