Nested arrays

Discussion in 'Perl Misc' started by Sandman, Mar 4, 2004.

  1. Sandman

    Sandman Guest

    Using php, an array definition might look like this:

    $foo = array(
    "foo" => array("foo", "bar"),
    "bar" => array("rab", "oof")
    );

    print $foo["foo"][0];

    ....would print "foo"

    $foo = array(
    "foo" => array("foo" => "orange", "bar" => "apple"),
    "bar" => array("rab" => "pear", "oof" => "pineapple")
    );

    print $foo["foo"][0];
    print $foo["foo"]["foo"];

    ....would both print "orange".

    How would the syntax be in perl? Perl differantiates betwen hashes and arrays,
    and in php an array is an hash and an array at the same time (i.e. every key in
    the array exists as both named and as index).

    --
    Sandman[.net]
    Sandman, Mar 4, 2004
    #1
    1. Advertising

  2. Sandman

    gnari Guest

    "Sandman" <> wrote in message
    news:-berlin.de...
    > Using php, an array definition might look like this:
    >
    > $foo = array(
    > "foo" => array("foo", "bar"),
    > "bar" => array("rab", "oof")
    > );
    >
    > print $foo["foo"][0];
    >
    > ...would print "foo"


    %foo =

    'foo' => ['foo','bar'],
    'bar' => ['rab','oof']
    );
    print $foo{'foo'}[0];

    prints "foo"


    >
    > $foo = array(
    > "foo" => array("foo" => "orange", "bar" => "apple"),
    > "bar" => array("rab" => "pear", "oof" => "pineapple")
    > );
    >
    > print $foo["foo"][0];
    > print $foo["foo"]["foo"];
    >
    > ...would both print "orange".


    %foo = (
    'foo' => {'foo' => 'orange' , 'bar => 'apple'},
    'bar' => {'rab' => 'pear' , 'rab => 'pimeapple'}
    );
    print $foo{'foo}{'foo'};

    prints "orange"

    print $foo{foo}[0];

    is an error;

    gnari
    gnari, Mar 4, 2004
    #2
    1. Advertising

  3. Sandman wrote:

    > Using php, an array definition might look like this:
    >
    > $foo = array(
    > "foo" => array("foo", "bar"),
    > "bar" => array("rab", "oof")
    > );
    >
    > print $foo["foo"][0];
    >
    > ...would print "foo"
    >
    > $foo = array(
    > "foo" => array("foo" => "orange", "bar" => "apple"),
    > "bar" => array("rab" => "pear", "oof" => "pineapple")
    > );
    >
    > print $foo["foo"][0];
    > print $foo["foo"]["foo"];
    >
    > ...would both print "orange".
    >
    > How would the syntax be in perl? Perl differantiates betwen hashes and
    > arrays, and in php an array is an hash and an array at the same time (i.e.
    > every key in the array exists as both named and as index).
    >


    See the Data Structures Cookbook
    http://www.perl.com/doc/FMTEYEWTK/pdsc/

    gtoomey
    Gregory Toomey, Mar 4, 2004
    #3
  4. Sandman <> wrote:

    > Using php, an array definition might look like this:
    >
    > $foo = array(
    > "foo" => array("foo", "bar"),
    > "bar" => array("rab", "oof")
    > );
    >
    > print $foo["foo"][0];
    >
    > ...would print "foo"
    >
    > $foo = array(
    > "foo" => array("foo" => "orange", "bar" => "apple"),
    > "bar" => array("rab" => "pear", "oof" => "pineapple")
    > );
    >
    > print $foo["foo"][0];
    > print $foo["foo"]["foo"];
    >
    > ...would both print "orange".
    >
    > How would the syntax be in perl? Perl differantiates betwen hashes and
    > arrays, and in php an array is an hash and an array at the same time
    > (i.e. every key in the array exists as both named and as index).


    You can answer this for yourself by reading one or more (preferably all) of
    perlreftut, perlref, and perldsc. Read perldsc first if you want to go
    straight to examples without all the information on references.

    Reading up on syntax is the easy part of learning a language. If syntax
    were all there was to it I'd already know Python and Ruby... but I don't.
    David K. Wall, Mar 4, 2004
    #4
  5. Also sprach Sandman:

    > Using php, an array definition might look like this:
    >
    > $foo = array(
    > "foo" => array("foo", "bar"),
    > "bar" => array("rab", "oof")
    > );
    >
    > print $foo["foo"][0];
    >
    > ...would print "foo"
    >
    > $foo = array(
    > "foo" => array("foo" => "orange", "bar" => "apple"),
    > "bar" => array("rab" => "pear", "oof" => "pineapple")
    > );
    >
    > print $foo["foo"][0];
    > print $foo["foo"]["foo"];
    >
    > ...would both print "orange".
    >
    > How would the syntax be in perl? Perl differantiates betwen hashes and arrays,
    > and in php an array is an hash and an array at the same time (i.e. every key in
    > the array exists as both named and as index).


    In Perl, arrays and hashes are two distinct data-types and you cannot
    access one as though it were the other. Fortunately there is never a
    need for such androgynous types. Your first example becomes:

    my %foo = (
    foo => [ qw/foo bar/ ],
    bar => [ qw/rab oof/ ],
    );

    print $foo{foo}[0];
    # which is really
    print $foo{foo}->[0]

    The second one:

    my %foo = (
    foo => { foo => 'orange', bar => 'apple' },
    bar => { rab => 'pear', oof => 'pineapple' },
    );

    print $foo{foo}{foo};
    # or
    print $foo{foo}->{foo};

    See 'perldoc perldsc' which tells you about creating compound
    data-structures. A preliminary 'perldoc perlref' or/and 'perldoc
    perlreftut' can't hurt in case you are not yet familiar with references
    which are involved here.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Mar 5, 2004
    #5
  6. Sandman

    Sandman Guest

    In article <c29a5a$8cd$-Aachen.DE>,
    "Tassilo v. Parseval" <> wrote:

    > In Perl, arrays and hashes are two distinct data-types and you cannot
    > access one as though it were the other. Fortunately there is never a
    > need for such androgynous types. Your first example becomes:
    >
    > my %foo = (
    > foo => [ qw/foo bar/ ],
    > bar => [ qw/rab oof/ ],
    > );
    >
    > print $foo{foo}[0];
    > # which is really
    > print $foo{foo}->[0]
    >
    > The second one:
    >
    > my %foo = (
    > foo => { foo => 'orange', bar => 'apple' },
    > bar => { rab => 'pear', oof => 'pineapple' },
    > );
    >
    > print $foo{foo}{foo};
    > # or
    > print $foo{foo}->{foo};


    Thanks!

    --
    Sandman[.net]
    Sandman, Mar 5, 2004
    #6
  7. Against PHP, hashes are widely used in Perl.

    And yes, Perl makes a certain difference between hashes and arrays.
    Using arrays and hashes in Perl is easy. Complexity begins when you
    continue to work with references to these data types.

    It can be powerful to combine these two data types, e.g. you might
    assign a reference of an array to a hash key. The benefit of the
    performance might be worth the cost of complexity.

    Find some code examples of nested hashes in Perl on
    http://www.infocopter.com/perl/hashes.htm

    Recursion is of unlimited depth as the general philosophy in Perl itself.

    --retoh :)
    --



    "Sandman" <> wrote in message
    news:-berlin.de...
    > Using php, an array definition might look like this:
    >
    > $foo = array(
    > "foo" => array("foo", "bar"),
    > "bar" => array("rab", "oof")
    > );
    >
    > print $foo["foo"][0];
    >
    > ...would print "foo"
    >
    > $foo = array(
    > "foo" => array("foo" => "orange", "bar" => "apple"),
    > "bar" => array("rab" => "pear", "oof" => "pineapple")
    > );
    >
    > print $foo["foo"][0];
    > print $foo["foo"]["foo"];
    >
    > ...would both print "orange".
    >
    > How would the syntax be in perl? Perl differantiates betwen hashes and

    arrays,
    > and in php an array is an hash and an array at the same time (i.e. every

    key in
    > the array exists as both named and as index).
    >
    > --
    > Sandman[.net]
    Reto Hersiczky, Mar 7, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,099
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    647
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    413
  4. Ultrus
    Replies:
    3
    Views:
    361
    Stefan Behnel
    Jul 9, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,105
    Philipp
    Jan 20, 2009
Loading...

Share This Page