Loading ...
Sorry, an error occurred while loading the content.

key reference inside of a hash

Expand Messages
  • Erwin Krause
    Hello, I m new on this list and perl and hope you can help me out with a little problem. I have a little problem with a hash / key reference. I have two
    Message 1 of 7 , Jul 2 2:39 AM
    • 0 Attachment
      Hello,

      I'm new on this list and perl and hope you can help me out with a little
      problem.

      I have a little problem with a hash / key reference.
      I have two hashes, where 1 hash stores a configuration and the other one
      stores the description for some keys / values of %h1

      my %config = {
      'ip' => 'value1',
      'netm' => 'value2',
      'bcast' => 'value3',
      }

      my %configdesc = {
      'ip' => "this is value1 !",
      'netm' => "this is value1 !",
      }



      than in my code i ask the user for the values I have a key in %h2.
      sub ask_config {
      foreach (keys %configdesc) {
      if ($config{$_}) {
      printf gettext "Please enter %s", $configdesc{$_};
      print "\n";

      # print default value for array and scalars
      if (ref($config{$_}) eq "ARRAY") {
      printf gettext "(default: %s) : ", join(' ',
      @{$config{$_}});
      } else {
      printf gettext "(default: %s) : ", join(' ', $config{$_});
      }
      my $input = <STDIN>; chomp($input);

      # array handling
      if (ref($config{$_}) eq "ARRAY") {
      my $key = $_;
      if ($input ne '') {
      my @tmp = split(/ /, $input);
      $config{$key} = @tmp;
      }
      } else {
      $config{$_} = $input if ($input ne '');
      }
      }
      }
      }



      So now my problem is, that i have to ask the user in a specific order.
      (ip before netmask, a.s.o.)
      I thought i simply can change things like that: (i tested both ways,
      1_ip and 2_netm)

      my %config = {
      'ip' => 'value1',
      'netm' => 'value2',
      'bcast' => 'value3',
      '1_ip' => \$config{'ip'},
      '2_netm' => sub { return $config{'ip'} }
      '2_netm' => \&get_hostname,
      }

      my %configdesc = {
      '1_ip' => "this is value1 !",
      '2_netm' => "this is value1 !",
      }

      with:
      sub get_hostname {
      return ${$config{'hostname'}};
      }



      both require to disable subs or refs in strict what is not the best way,
      i know.

      Some1 have an idea to fix this problem or maybe do the whole thing some
      better?
      I dont wanna rewrite my whole code to change the config{ip} to config{1_ip}.


      Best regards and thanks for your efforts.

      Erwin
    • merlyn@stonehenge.com
      ... Erwin Hello, Erwin I m new on this list and perl and hope you can help me out with a little Erwin problem. Erwin I have a little problem with a hash /
      Message 2 of 7 , Jul 2 7:56 AM
      • 0 Attachment
        >>>>> "Erwin" == Erwin Krause <devnull@...> writes:

        Erwin> Hello,
        Erwin> I'm new on this list and perl and hope you can help me out with a little
        Erwin> problem.

        Erwin> I have a little problem with a hash / key reference.
        Erwin> I have two hashes, where 1 hash stores a configuration and the other one
        Erwin> stores the description for some keys / values of %h1

        Erwin> my %config = {
        Erwin> 'ip' => 'value1',
        Erwin> 'netm' => 'value2',
        Erwin> 'bcast' => 'value3',
        Erwin> }

        This is not a hash. Either:

        my $config = { ... };

        for a hashref, or

        my %config = ( ... );

        for a real hash.

        Don't mix the forms.

        If you had warnings on (and why didn't you), it would have said:

        $ perl -we 'my %foo = { hello => 3 }'
        Reference found where even-sized list expected at -e line 1.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      • Erwin Krause
        Message 3 of 7 , Jul 2 11:06 AM
        • 0 Attachment
          Randal L. Schwartz schrieb:
          >>>>>> "Erwin" == Erwin Krause <devnull@...> writes:
          >>>>>>
          >
          > Erwin> Hello,
          > Erwin> I'm new on this list and perl and hope you can help me out with a little
          > Erwin> problem.
          >
          > Erwin> I have a little problem with a hash / key reference.
          > Erwin> I have two hashes, where 1 hash stores a configuration and the other one
          > Erwin> stores the description for some keys / values of %h1
          >
          > Erwin> my %config = {
          > Erwin> 'ip' => 'value1',
          > Erwin> 'netm' => 'value2',
          > Erwin> 'bcast' => 'value3',
          > Erwin> }
          >
          > This is not a hash. Either:
          >
          > my $config = { ... };
          >
          > for a hashref, or
          >
          > my %config = ( ... );
          >
          > for a real hash.
          >
          > Don't mix the forms.
          >
          > If you had warnings on (and why didn't you), it would have said:
          >
          > $ perl -we 'my %foo = { hello => 3 }'
          > Reference found where even-sized list expected at -e line 1.
          >
          >
        • Erwin Krause
          Erwin Hello, Erwin I m new on this list and perl and hope you can help me out with a little Erwin problem. Erwin I have a little problem with a hash / key
          Message 4 of 7 , Jul 2 9:48 PM
          • 0 Attachment
            Erwin> Hello,
            Erwin> I'm new on this list and perl and hope you can help me out with a little
            Erwin> problem.

            Erwin> I have a little problem with a hash / key reference.
            Erwin> I have two hashes, where 1 hash stores a configuration and the other one
            Erwin> stores the description for some keys / values of %h1

            Erwin> my %config = {
            Erwin> 'ip' => 'value1',
            Erwin> 'netm' => 'value2',
            Erwin> 'bcast' => 'value3',
            Erwin> }

            This is not a hash. Either:

            my $config = { ... };

            for a hashref, or

            my %config = ( ... );

            for a real hash.

            Don't mix the forms.

            If you had warnings on (and why didn't you), it would have said:

            $ perl -we 'my %foo = { hello => 3 }'
            Reference found where even-sized list expected at -e line 1.




            Yeah you are right, I used ( ) in my code, just wrote this down and dont
            c&p that part, so I did the mistake.

            I received an e-mail with the tip to just use an order array. (thanks
            for that Bill :))
            my @config_order = ('ip', 'netm');
            i'm using this, it should work for my problem.

            But now I'm having another problem ^^
            It would be nice, to trigger a function on every change of $config{'ip'}
            that update the broadcast and gw so that the user just have to hit
            enter, because the default value was calculated from the netm and ip.

            I didn't find anything about forward declaration, so that i can use the
            $config{''} hash in a nested function like
            $config = (
            ip => "192.168.1.1",
            netmask => "255.255.255.0",
            gw => sub{ some calculation with $config{'ip'} and $config{'netmask'}},
            );

            Cause I'm doing all the userinput in a loop and dont wanna do something
            like
            foreach keys %config {
            ask_bla
            if ($_ eq "ip"){
            update_gw;
            }
            }

            mybe someone had an idea for this.

            best regards
            Erwin
          • merlyn@stonehenge.com
            ... Erwin Yeah you are right, I used ( ) in my code, just wrote this down and dont Erwin c&p that part, so I did the mistake. I ve stopped helping you now.
            Message 5 of 7 , Jul 2 10:40 PM
            • 0 Attachment
              >>>>> "Erwin" == Erwin Krause <devnull@...> writes:

              Erwin> Yeah you are right, I used ( ) in my code, just wrote this down and dont
              Erwin> c&p that part, so I did the mistake.

              I've stopped helping you now. You aren't pasting things that
              are consistent with your problem.

              For example, you go on later to do this:

              Erwin> $config = (
              Erwin> ip => "192.168.1.1",
              Erwin> netmask => "255.255.255.0",
              Erwin> gw => sub{ some calculation with $config{'ip'} and $config{'netmask'}},
              Erwin> );

              And this is *also* broken syntax.

              If you WANT HELP,
              please paste REAL CODE.

              Stop typing in random code. PASTE THE ACTUAL CODE YOU ARE WONDERING ABOUT.

              Yes. It matters.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
              See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
            • Erwin Krause
              Mh I m really sorry, but i dont have an syntax problem. But i can make this more imaginable #!/usr/bin/perl use strict; use warnings; %config = ( ip =
              Message 6 of 7 , Jul 3 7:36 AM
              • 0 Attachment
                Mh I'm really sorry, but i dont have an syntax problem.
                But i can make this more imaginable

                #!/usr/bin/perl
                use strict;
                use warnings;
                %config = (
                ip => "192.168.1.1",
                netmask => "255.255.255.0",
                gw => sub{ print "this will fail: $config{'ip'}\n";},
                );
                0;

                I thought when i talk about forward declaration.
                And Yes, I'm not a perl programmer, I usually program code that doesn't
                use %,$,@ in front of the same variable. and there for I maybe
                misspelled this. And even its a little problem atm this is more a
                general question.
                I had also just ask about forward declarations or something like that.
                But I will test my posted code in future in a seperate programm and obey
                this rule.

                Best regards
                Erwin



                Randal L. Schwartz schrieb:
                > I've stopped helping you now. You aren't pasting things that
                > are consistent with your problem.
                >
                > For example, you go on later to do this:
                >
                > Erwin> $config = (
                > Erwin> ip => "192.168.1.1",
                > Erwin> netmask => "255.255.255.0",
                > Erwin> gw => sub{ some calculation with $config{'ip'} and $config{'netmask'}},
                > Erwin> );
                >
                > And this is *also* broken syntax.
                >
                > If you WANT HELP,
                > please paste REAL CODE.
                >
                > Stop typing in random code. PASTE THE ACTUAL CODE YOU ARE WONDERING ABOUT.
                >
                > Yes. It matters.
                >
              • merlyn@stonehenge.com
                ... Erwin Mh I m really sorry, but i dont have an syntax problem. Erwin But i can make this more imaginable Erwin #!/usr/bin/perl Erwin use strict; Erwin
                Message 7 of 7 , Jul 3 8:35 AM
                • 0 Attachment
                  >>>>> "Erwin" == Erwin Krause <devnull@...> writes:

                  Erwin> Mh I'm really sorry, but i dont have an syntax problem.
                  Erwin> But i can make this more imaginable

                  Erwin> #!/usr/bin/perl
                  Erwin> use strict;
                  Erwin> use warnings;
                  Erwin> %config = (
                  Erwin> ip => "192.168.1.1",
                  Erwin> netmask => "255.255.255.0",
                  Erwin> gw => sub{ print "this will fail: $config{'ip'}\n";},
                  Erwin> );
                  Erwin> 0;

                  my %config;
                  %config = ( .... );

                  That'll work for what you want.

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                  See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                Your message has been successfully submitted and would be delivered to recipients shortly.