Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Nested arrays (http://www.velocityreviews.com/forums/t885609-nested-arrays.html)

Sandman 03-04-2004 10:19 PM

Nested arrays
 
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]

gnari 03-04-2004 11:07 PM

Re: Nested arrays
 
"Sandman" <mr@sandman.net> wrote in message
news:mr-12CDF3.23193804032004@news.fu-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




Gregory Toomey 03-04-2004 11:38 PM

Re: Nested arrays
 
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

David K. Wall 03-04-2004 11:47 PM

Re: Nested arrays
 
Sandman <mr@sandman.net> 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.


Tassilo v. Parseval 03-05-2004 07:27 AM

Re: Nested arrays
 
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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval

Sandman 03-05-2004 01:05 PM

Re: Nested arrays
 
In article <c29a5a$8cd$1@nets3.rz.RWTH-Aachen.DE>,
"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> 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]

Reto Hersiczky 03-07-2004 05:48 PM

Re: Nested arrays
 
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" <mr@sandman.net> wrote in message
news:mr-12CDF3.23193804032004@news.fu-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]





All times are GMT. The time now is 04:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.