|
Hi,
I have to concatenate a string to a variable. This a piece of my code.
@Category = qw(abc def ghi);
while($line = <IN1>) {
chomp $line;
foreach $search (@Category)
{
if ($line =~ /^$search/){
push @{$HASH{"$search._string"}}, $value;
}
}
}
Result should be like
%HASH { abc_string => $value,
def_string => $value,
ghi_string => $value};
Instead I am getting somethign like this,
%HASH { abc._string => $value,
def._string => $value,
ghi._string => $value};
Isn't "." concatenation operator?
What is the correct way to concatenate a string to a variable?
Thanks
Toshi
|
|
|
just show us exactly what the problem is instead of throwing the entire program at us.
|
|
|
The question does not contain the entire code.
But I got your point, let me rewrite the problem without specifying too much detail.
Problem:
I have to concatenate a 'string' to a 'variable'.
example, concatenate $var to '_abc'
say $var = file
Operator "." is used to do so.
Expected result is file_abc. Instead I am getting "file._abc".
I have used the following syntax:
push @{$HASH{"$search._string"}}, $value;
Please suggest a way to get the desired result.
Thanks
Toshi
|
|
|
#thats a majorly complicated way to do a simple thing.
#concatenate $var to '_abc'
#say $var = file
#Operator "." is used to do so.
$somevar = "hello";
$another_var = " there";
$both_vars = "$somevar"."$another_var";
#$both_vars now has "hello there".
|
|
|
for simplicity and to break the problem into smaller peaces,
declare the variable before you search for it.
so instead of:
push @{$HASH{"$search._string"}}, $value;
have:
$to_search_for = "$search"."_string";
push @{$HASH{"$to_search_for"}}, $value;
#that way it will be easier to error check.
|
|
|
maybe skip the quotes so it would be:
push @{$HASH{$to_search_for}}, $value;
|
|
|
Thank you! It worked.
Regards
Toshi
|
|
|
Nice post about variable using perl. I like your blog very much because it has very interesting articles of different topics like testking HP0-J22, testking 642-611 and testking 642-062 and their tips and tricks. I am a very big googler and search on different topics. Between searching i found your nice blog. Thanks for your this great blog.
|
|
|