LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2012, 10:01 AM   #1
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Rep: Reputation: Disabled
perl array nof hash


suppose $x is a reference to an array of hash then does @y is the array equivalent for the array of hash.


@y = @($x); # typecasting it to an array

and does $y[0]{"some key"} is the proper way to access the elements of the array??



please help
 
Old 08-25-2012, 10:07 AM   #2
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
i meant does @y is an array of hash??
 
Old 08-26-2012, 06:44 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
@ = array
% = hash
$ = scalar (single value)

These are the 3 (main) var types in Perl.
Hence an array @arr1 has first element $arr1[0] because the element is a scalar.

What you are talking about is $x is a ref-to-array, each element is a ref-to-hash, so @y = array of hash-refs

Try reading this http://perldoc.perl.org/perldsc.html and indeed bookmark the main site; it's full of good stuff
 
Old 08-26-2012, 11:37 PM   #4
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
i didnt mean it......i had asked that if $x is a refernce to an array of hash then what does the below mean

@y= @($x);

does it mean that @y is now the actual array of hash whose refernce was held in $x



this is my question

thanks friends
 
Old 08-27-2012, 02:30 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,177

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
I think the "cast" works as:
@y = @{$x};
this is what you are looking for. See this page about using references: http://perldoc.perl.org/perlref.html
 
Old 08-27-2012, 03:12 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Like I said in post #3 (& see my link)

$x is a ref-to-an-array, each element of which is a ref-to-a-hash, so @y = array of hash-refs.

IOW,
Code:
@y= @($x);
effectively takes $x and 'casts it' to an actual array @y (each element of which is a ref-to-a-hash)
 
  


Reply


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
Perl Hashes -- Updating a hash ref via hash value 0.o Programming 5 06-05-2012 12:45 PM
Perl Hash of Hash reference query kdelover Programming 1 02-19-2011 04:47 AM
[SOLVED] Perl - How to store and access an array in a hash of hashes? DevonB Programming 4 07-07-2010 07:57 AM
LXer: Hash Suffix Array Delta Compression on Linux LXer Syndicated Linux News 0 11-28-2007 07:50 PM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM


All times are GMT -5. The time now is 05:44 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration