Find the answer to your Linux question:
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 ...
  1. #1
    Linux 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

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The code that you wrote is not valid. I will assume that you meant this:
    Code:
    my $data1 = {};
    my $ret = function($data1, ..other..);
    (note the addition of an equals sign to the first line)

    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

  3. #3
    Linux Newbie
    Join Date
    Dec 2008
    Location
    Luxembourg
    Posts
    130
    Dear Cabhan,
    thanks a lot. You save my day

    Bye,
    Bruno

  4. #4
    Linux 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

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  6. #6
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...