temp string manipulation :

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    temp string manipulation :

    HI,

    i'd like to use regexp replacement without changing variable value.
    for example : in the following code i'd like to preseve the value of $key, but i need to change it's value in order to use it as an hash key. however, the hash key is only temporal and can be destroyed right after getting the hash.

    Code:
     
    my $key   = "aaa.bbb.ccc"; 
    my $temp =~ s/\.ccc//; 
    print $hash{$temp};
    the question is whether i can perform the above code without using the $temp.

    thanks,
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    I can understand why you would want to do such a thing,
    but I can not think of a stright-forward way to avoid
    a temporary variable. A temp variable isn't so bad
    in your case.

    I can, however, think up a couple of convoluted ways
    to avoid a temp var. One way is to use split:

    Code:
    my $key = "aaa.bbb.ccc";
    print $hash{(split /\.ccc/, $key)[0]};
    I don't recommend this, since a temp var
    is much less obscure, in my opinion.

    I won't even show you my File::Basename solution :)

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      I have to agree with toolic. The temp variable is not at all a bad solution. Why are you so averse to it?

      Regards,

      Jeff

      Comment

      Working...