Results 1 to 6 of 6
Hello,
I have a code containing
...
my $data1{};
my $ret = function($data1, ..other..);
...
As far as I undertand data1 is an associative array.
How can I parse $data1 ...
- 07-08-2009 #1Linux Newbie
- Join Date
- Dec 2008
- Location
- Luxembourg
- Posts
- 130
Perl: what is 'my $data1{}'
Hello,
I have a code containing
...
my $data1{};
my $ret = function($data1, ..other..);
...
As far as I undertand data1 is an associative array.
How can I parse $data1 ?
I do not know its structure.
Thanks for any clue.
Bye,
Bruno
- 07-08-2009 #2
The code that you wrote is not valid. I will assume that you meant this:
(note the addition of an equals sign to the first line)Code:my $data1 = {}; my $ret = function($data1, ..other..);
In this case, $data1 is what we call a hashref, or a reference to a hash / associative array. $data1 is a scalar, but when dereferenced, produces a hash. If you don't already know about references, I suggest learning about them: they are a bit complicated, but in more advanced Perl, you use them all the time.
To print a hashref, you might do something like this:
Code:my @keys = sort(keys(%$data1)); for my $key (@keys) { print "$key -> ", $data1->{$key}, "\n"; }DISTRO=Arch
Registered Linux User #388732
- 07-09-2009 #3Linux Newbie
- Join Date
- Dec 2008
- Location
- Luxembourg
- Posts
- 130
Dear Cabhan,
thanks a lot. You save my day
Bye,
Bruno
- 07-10-2009 #4Linux Newbie
- Join Date
- Dec 2008
- Location
- Luxembourg
- Posts
- 130
As I'm able now to display values in mydata1, ie:
CodedCharSetId -> 850
How can I modify a value in it ?
I continue to struggle with the syntax and prefix $ or % ..etc..
Thanks for help
Bruno
- 07-10-2009 #5
Once again, I would suggest finding a good tutorial on references for all of this information.
In any event, I showed you how to get a value from a hashref: assigning a value is pretty obvious at that point:
Code:print $hashref->{$key}; $hashref->{$key} = "value";DISTRO=Arch
Registered Linux User #388732
- 09-04-2009 #6Linux Newbie
- Join Date
- Dec 2008
- Location
- Luxembourg
- Posts
- 130
Dear Cabhan,
thanks a lot again for your help.
Just to keep you in touch about your (good) advices as I indeed bought some Perl books:
- 'Learning Perl' by Randal Schwartz (aka 'The Llama book')
- 'Programming Perl' by Larry Wall (I suppose aka 'The Camel book')
I found 'Learning Perl' is a great Perl book, I really enjoy reading it, and I must admit that I discover (and now love) Perl by reading it.
Hopefully my next questions on the forum will be more accurate.
Bye,
Bruno


Reply With Quote