LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09-04-2007, 05:32 PM   #1
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
How to access a class property that is a hash


Hi all

I am trying to access a hash that is a member of a the class.

It tells me, however, that:

Can't coerce array into hash

Here is the code
Code:
package llexample
	sub new {

		my $strClass=shift;

		my $self={

			'strConnectionName'=>shift,
			'intProtocolVersion' => (((shift)-0)==0x34?0x34:0x33),
			'strHostName' => shift,
			'strUserName' => shift,
			'strPassword' => shift,
			'hashNetworks' => shift
		};

		my $objOut=bless $self,$strClass;

		return $objOut;
	}

	sub showhash {

		my $this=shift;

		while(my $key=keys %{$this->{'hashNetworks'}}) {

			print "$key\n";
		}
	}
}

my $c=new llExample('param',0x33,'param','param','param',\@myhash);

$c->showhash();
Will throw the error on the line that has the 'while'.

Can somebody please tell me the correct way to access a hash that is a property of a class.
 
Old 09-04-2007, 08:07 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It's not clear which language you are using... sub's is this basic?
or maybe Perl?

Last edited by graemef; 09-04-2007 at 08:15 PM.
 
Old 09-04-2007, 08:21 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd say Perl, but in the call my $c=new llExample... the last param (\@myhash) is being passed as an array ref, but named as 'myhash'.
I'd say that's wrong for a start?
 
Old 09-05-2007, 01:35 AM   #4
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Original Poster
Rep: Reputation: 38
Sorry it is Perl indeed. And I do have some issues grasping the different types of perl. I thought I was using a hash but it is actually an array of hashes. . .
 
Old 09-05-2007, 03:19 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, I've never messed with OO Perl, so I'll pass (although correct var naming definitely helps in debugging).
These guys ( http://www.perlmonks.org/ ) are the experts.
 
Old 09-05-2007, 09:43 AM   #6
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Original Poster
Rep: Reputation: 38
Thanks for the link
 
  


Reply



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
Can I access class A and class C ip address using one ethernet card? fakhrul Linux - Networking 4 10-21-2007 01:34 AM
JSP access java system class davnetuk Programming 1 03-06-2007 03:23 PM
JavaScript: Changing a class property tumana Programming 2 02-15-2006 01:35 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
sendmail-8.12.11 hash class error tanhs Red Hat 0 02-27-2004 12:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

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
Open Source Consulting | Domain Registration