Connecting Tech Pros Worldwide Help | Site Map

temp string manipulation :

Member
 
Join Date: Jan 2008
Posts: 48
#1: Oct 1 '09
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. my $key   = "aaa.bbb.ccc"; 
  3. my $temp =~ s/\.ccc//; 
  4. print $hash{$temp}; 
  5.  
the question is whether i can perform the above code without using the $temp.

thanks,
Newbie
 
Join Date: Sep 2009
Posts: 15
#2: Oct 3 '09

re: temp string manipulation :


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:

Expand|Select|Wrap|Line Numbers
  1. my $key = "aaa.bbb.ccc";
  2. print $hash{(split /\.ccc/, $key)[0]};
  3.  
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 :)
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#3: Oct 4 '09

re: temp string manipulation :


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
Reply