codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How to concatenate a string and a variable using perl?  toshimathur at 10:34 on Friday, September 07, 2007
 

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


  Re: How to concatenate a string and a variable using perl?  hermanningjaldsson at 18:43 on Friday, September 07, 2007
 

just show us exactly what the problem is instead of throwing the entire program at us.



  Re: How to concatenate a string and a variable using perl?  toshimathur at 06:26 on Saturday, September 08, 2007
 

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

  Re: How to concatenate a string and a variable using perl?  hermanningjaldsson at 09:44 on Saturday, September 08, 2007
 

#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".



  Re: How to concatenate a string and a variable using perl?  hermanningjaldsson at 12:18 on Saturday, September 08, 2007
 

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.





  Re: How to concatenate a string and a variable using perl?  hermanningjaldsson at 13:26 on Saturday, September 08, 2007
 

maybe skip the quotes so it would be:
push @{$HASH{$to_search_for}}, $value;



  Re: How to concatenate a string and a variable using perl?  toshimathur at 05:46 on Monday, September 10, 2007
 

Thank you! It worked.

Regards
Toshi

  Re: How to concatenate a string and a variable using perl?  arash farhad at 11:57 on Wednesday, March 10, 2010
 

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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Difference between two dates including From & To date
•  Disabe onbeforeunload event?
•  Re: MS Access connection in JSP
•  Re: execute linux commands in perl script
•  Re: Server Name or Address could not be resolved?
•  Re: is null or not an object error
•  Re: Using Crystal Reports for generating dynamic reports in JAVA
•  Re: how to connect asp.net to sql server 2000
•  Re: Dissolve effect for images using Javascript!


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010