How to replace the predefined variable which is set after the decleration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yash0101
    New Member
    • Aug 2009
    • 3

    How to replace the predefined variable which is set after the decleration

    I have a hash , and in the value part of that (which is a string , So that variable interpolation can happen) i have used a variable .
    Now later in the program i set this variable and try to substitute the value of it in the value of hash but it is returning null;

    Code:
    %Hash=('add'=> " test  $type ");
    $type = 1000;
    
    print " $Hash{$type};";
    Last edited by numberwhun; Aug 15 '09, 04:59 AM. Reason: Please use code tags!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by yash0101
    I have a hash , and in the value part of that (which is a string , So that variable interpolation can happen) i have used a variable .
    Now later in the program i set this variable and try to substitute the value of it in the value of hash but it is returning null;

    Code:
    %Hash=('add'=> " test  $type ");
    $type = 1000;
    
    print " $Hash{$type};";
    You have to define the variable $type before you use it somewhere, otherwise, the place where its used will be filled with a null value.

    My suggestion is that you add the following to the beginning of your script:

    Code:
    use strict;
    use warnings;
    If you had them in place, you would have seen the following output to your screen:

    Code:
    Global symbol "%Hash" requires explicit package name at hashtest.pl line 20.
    Global symbol "$type" requires explicit package name at hashtest.pl line 20.
    Global symbol "$type" requires explicit package name at hashtest.pl line 21.
    Global symbol "%Hash" requires explicit package name at hashtest.pl line 23.
    Global symbol "$type" requires explicit package name at hashtest.pl line 23.
    Execution of hashtest.pl aborted due to compilation errors.
    Also, this line:

    Code:
    print " $Hash{$type};";
    is not correct. The first semi-colon needs to be rmoved so it looks like this:

    Code:
    print " $Hash{$type}";
    Another issue is that $type is the value and the way you are referencing it is as if it were a key, so you will always get the following error:

    Code:
    Use of uninitialized value within %Hash in concatenation (.) or string at hashtest.pl line 24.
    To have this code work the way you expect it, you will have to have it as follows:

    Code:
    use strict;
    use warnings;
    
    
    # Variables
    my $type = "1000";
    my %Hash=('add' => " test  $type ");
    
    
    #print " $Hash{$type}";
    print("$Hash{add}");
    I hope this helps.

    Regards,

    Jeff

    Comment

    • yash0101
      New Member
      • Aug 2009
      • 3

      #3
      Thanks jeff ,
      and print " $Hash{$type};"; was a typing mistake.

      And what actually my problem is that i want to make the code more generic and to do that i use the variable inside the hash and i replace that variable at the runtime.

      So, because of this reason i cant hard code the $type before the hash.

      What i want is something like if i can some how refer to the variable and can change the value stored in that variable at the runtime .

      Anyways thansk for the reply.
      :)

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        You can change the value of a variable, but not when it is used in a string. When you put a variable in a string its value replaces the variable, and then perl builds the string. The variable is not stored in the string only its value.

        Comment

        • yash0101
          New Member
          • Aug 2009
          • 3

          #5
          Thanks KevinADC for the reply..

          Comment

          Working...