By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

temp string manipulation :

P: 51

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
  2. my $key   = "aaa.bbb.ccc"; 
  3. my $temp =~ s/\.ccc//; 
  4. print $hash{$temp}; 
the question is whether i can perform the above code without using the $temp.

Oct 1 '09 #1
Share this Question
Share on Google+
2 Replies

P: 70
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]};
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 :)
Oct 3 '09 #2

Expert Mod 2.5K+
P: 3,503
I have to agree with toolic. The temp variable is not at all a bad solution. Why are you so averse to it?


Oct 4 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.