LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-15-2012, 04:59 PM   #1
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Rep: Reputation: Disabled
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..
 
Old 12-15-2012, 08:08 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 750
Blog Entries: 2

Rep: Reputation: 194Reputation: 194
"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"'
 
Old 12-15-2012, 09:01 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,268

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
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.

Last edited by sundialsvcs; 12-15-2012 at 09:02 PM.
 
Old 12-16-2012, 08:53 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,057
Blog Entries: 1

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
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.
 
Old 12-16-2012, 10:17 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by summer View Post
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 .
 
Old 12-18-2012, 05:48 PM   #6
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 12-18-2012, 06:07 PM   #7
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
"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..
 
Old 12-18-2012, 06:18 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by summer View Post
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
.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] 500 OOPS: vsftpd: refusing to run with writable anonymous root: Anonymous Login sanjay87 Linux - Server 5 08-14-2012 01:28 AM
Problems with anonymous login proftp 1.3.1: 530-Unable to set anonymous priviliges. gentooox Linux - Server 3 05-03-2009 02:46 PM
ProFTPD and Anonymous access *confused* yah0m Linux - Server 1 11-01-2008 10:59 AM
files uploaded to anonymous ftp not readable by anonymous TheOneAndOnlySM Linux - Software 2 11-04-2004 07:42 AM
passing anonymous hash in CGI hidden field wonderdog33 Programming 1 01-30-2004 11:19 AM


All times are GMT -5. The time now is 02:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration