LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Confused between anonymous array and anonymous hash (http://www.linuxquestions.org/questions/programming-9/confused-between-anonymous-array-and-anonymous-hash-4175441601/)

summer 12-15-2012 05:59 PM

Confused between anonymous array and anonymous hash
 
my $ref = [{Shu => 21}, {Zee => 22}, {Haa => 11}];

a) hash containing anonymous hashes.
b) anonymous array of references to anonymous hashes.
c) array of arrays
d) anonymous hash of hash references.
e) multi-dimensional array

i am confused between (b) and (d).Which one is appropriate. Can anyone please explain me the difference.
Thank You..

linosaurusroot 12-15-2012 09:08 PM

"man perlreftut" should help you out.

Also
Code:

perl -e 'my $ref = [{Shu => 21}, {Zee => 22}, {Haa => 11}]; print $ref, "\n"; print ${$ref}[0], "\n"'

sundialsvcs 12-15-2012 10:01 PM

First, check out http://www.perlmonks.org

Now on to your question: the variable $ref:
  • Is a scalar (since "$foobar")
  • Which contains a reference to an array (i.e. an "arrayref"),
  • Each element of which is a reference to a hash (a "hashref").

In Perl, an array is a one-dimensional structure whose elements are accessed by a zero-based numeric index.

A hash is a one-dimensional structure whose elements are accessed by string key-values, such as (in this case) "Shu," "Haa," and "Zee."

A reference is simply "a thing," specifically a scalar thing, which "refers to" something else ... that is to say, "indirectly." A reference can refer to any sort of thing.

It is a good idea to prefix all programs with: use strict; use warnings;. Do this as a strict matter of routine, every time. Perl is otherwise "do what I (think you) mean." It can allow you to write stuff that isn't what you thought you were writing, unless you, in this way, ask for more stricture and warnings. Do so.

onebuck 12-16-2012 09:53 AM

Moderator Response
 
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

Sergei Steshenko 12-16-2012 11:17 AM

Quote:

Originally Posted by summer (Post 4850066)
my $ref = [{Shu => 21}, {Zee => 22}, {Haa => 11}];

a) hash containing anonymous hashes.
b) anonymous array of references to anonymous hashes.
c) array of arrays
d) anonymous hash of hash references.
e) multi-dimensional array

i am confused between (b) and (d).Which one is appropriate. Can anyone please explain me the difference.
Thank You..

In Perl there are no (strictly speaking) anonymous arrays and there are no anonymous hashes.

Instead anonymous array references and anonymous hash references do exist.

For simplicity/brevity the above references are called anonymous arrays and anonymous hashes. Unofficially :).

summer 12-18-2012 06:48 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4850419)
In Perl there are no (strictly speaking) anonymous arrays and there are no anonymous hashes.

Instead anonymous array references and anonymous hash references do exist.

For simplicity/brevity the above references are called anonymous arrays and anonymous hashes. Unofficially :).

From your description and from all of the above description i came to a conclusion that it is an anonymous array of references to anonymous hashes..because i know for sure that anonymous array starts with a square bracket.

summer 12-18-2012 07:07 PM

Quote:

Originally Posted by linosaurusroot (Post 4850136)
"man perlreftut" should help you out.

Also
Code:

perl -e 'my $ref = [{Shu => 21}, {Zee => 22}, {Haa => 11}]; print $ref, "\n"; print ${$ref}[0], "\n"'

Thank you very much..it helped me alot..

Sergei Steshenko 12-18-2012 07:18 PM

Quote:

Originally Posted by summer (Post 4852279)
From your description and from all of the above description i came to a conclusion that it is an anonymous array of references to anonymous hashes..because i know for sure that anonymous array starts with a square bracket.

You just think you know. But you don't. Here is a one-liner:

Code:

sergei@amdam2:~> perl -e 'print ref([1,2]), "\n"'
ARRAY

- the 'ref' function tells it's a reference of type ARRAY. From 'perldoc -f ref':


Code:

      ref EXPR
      ref    Returns a non-empty string if EXPR is a reference

.


All times are GMT -5. The time now is 11:00 AM.