Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Nested arrays

Reply
Thread Tools

Nested arrays

 
 
Sandman
Guest
Posts: n/a
 
      03-04-2004
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]
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      03-04-2004
"Sandman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-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



 
Reply With Quote
 
 
 
 
Gregory Toomey
Guest
Posts: n/a
 
      03-04-2004
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
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      03-04-2004
Sandman <(E-Mail Removed)> 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.

 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      03-05-2004
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
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      03-05-2004
In article <c29a5a$8cd$(E-Mail Removed)-Aachen.DE>,
"Tassilo v. Parseval" <(E-Mail Removed)> 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]
 
Reply With Quote
 
Reto Hersiczky
Guest
Posts: n/a
 
      03-07-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-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]



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments