Nested arrays

S

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).
 
G

gnari

Sandman said:
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
 
G

Gregory Toomey

Sandman said:
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
 
D

David K. Wall

Sandman said:
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.
 
T

Tassilo v. Parseval

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
 
S

Sandman

Tassilo v. Parseval said:
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!
 
R

Reto Hersiczky

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top