in reply to Creating a unique variable type

You could probably use an array of arrays (AoA), an array of hashes (AoH), an hash or arrays (HoA) or a hash of hashes (HoH). Your could even use 2 arrays, or 2 hashes, or an array and a hash, but that is sort of less interesting. You could even use a simple array (or a simple hash) and concatenate the Boolean and the int into it, but that starts to be quite ugly.

Replies are listed 'Best First'.
Re^2: Creating a unique variable type
by tfredett (Sexton) on Mar 25, 2013 at 20:25 UTC
    I was under the impression you couldn't place an array inside another array with Perl, am I perhaps incorrect in this assumption? I only say this, because I actually considered doing that using that in a similar sense, but after some research, found that Perl doesn't really like doing that.

      Technically, you are correct. An array value can only hold a scalar variable.

      However, there are a couple of ways you can work around that. The most common is references. Instead of storing an array inside of another array, you store array references (which are scalar values) in your array.

      The easiest way would be an array of array references:

      my @array = ( "a", "b", "c" ); my $aref = [ "x", "y", "z" ]; push @array, $aref; # Current value for @array: #@array = ( # "a", # "b", # "c", # [ # "x", # "y", # "z" # ], # )

      A brief example of using an array of array references:

      #!/usr/bin/env perl use strict; use warnings; my @array; while (<DATA>) { chomp; my ($bool,$int) = split ","; # Take each ($bool,$int) tuple, and store it in an array reference # (By placing it in []) that we add to our array push @array, [ $bool, $int ]; } # Cycle through our array of array references and print them foreach my $entry (@array) { print "Bool: " . $entry->[0] . "\n"; # The -> dereferences our ar +rayref print "Int: " . $entry->[1] . "\n\n"; # The -> dereferences our ar +rayref } __DATA__ 0,14 0,7 1,13 0,3 1,9 0,2 1,1

      Giving us the following output:

      perl-fu@nexus:~/foo$ ./ Bool: 0 Int: 14 Bool: 0 Int: 7 Bool: 1 Int: 13 Bool: 0 Int: 3 Bool: 1 Int: 9 Bool: 0 Int: 2 Bool: 1 Int: 1

      For more information on nested data structures using references, you should check out perlreftut and perlref.

      Christopher Cashell

      Your are right, you can't really place an array inside another array with Perl, but you can put in an array a reference toward another array. And you can do it in a way that you don't have to be explicit about the existence of a reference, it really looks like a two-dimensional array.

      Suppose you want to store in an array the full name and the abbreviated name of each of the months. Your could do this in various syntactic ways as follows:

      $month[0][0] = "January"; $month[0][1] = "Jan"; $month[1] = ["February", "Feb"]; push @month, ["March", "Mar"]; push @{$month[3]}, "April"; push @{$month[3]}, "Apr";
      If you dump the content of @month under the debugger, you get the following structure:
      0 ARRAY(0x20381390) 0 'January' 1 'Jan' 1 ARRAY(0x20381528) 0 'February' 1 'Feb' 2 ARRAY(0x203815d0) 0 'March' 1 'Mar' 3 ARRAY(0x204424d0) 0 'April' 1 'Apr'

      The dump shows that the @month array contains 4 references to arrays (the 'ARRAY(0x20381390)' part), and that each of these arrays contain two elements, the month full and short names. And you can just print one element as follows:

      print $month[3][1]; # prints "Apr"

      So you could almost use all this without even knowing about references. Except that if you don't understand the underlying references, you will not be able to understand what's happening when something goes wrong (say, when the compiler complains about some illegal construct or, worse, when your program is not doing what you want because your data is not quite what you think).

      In brief, you can in principle construct an entire program with AoA (or AoH, HoA or HAH) without ever using a syntax that explicitly shows that the top-level array contains in fact references to other (anonymous) arrays. But you still need to know and understand this reference shebang if you are going to use it for anything else than an extremely simple program.

      > I was under the impression you couldn't place an array inside another array with Perl, am I perhaps incorrect in this assumption?

      Of course you can, the irritation stems from the fact that Perl has two representations of complex variables, the standard "list" form with sigil and the "scalar" reference form.

      You need to use the reference form, because elements need to be scalars.

      Cheers Rolf

      ( addicted to the Perl Programming Language)