Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list concatenation

Status
Not open for further replies.

EchoCola

Programmer
Apr 13, 2004
48
US
Hey guys, wanted to know how to concatenate two lists. I looked around and only saw examples using scalaras with the .= operation.
I would like to concatenate for example:
Code:
@list_1 = ('my','dog');
@list_2 = ('is','lazy');
@final_list = "insert code here"

print @final_list;

Output: "my dog is lazy"
 
Try this:

Code:
my @list_1 = ('my','dog');
my @list_2 = ('is','lazy');
my @final_list = (@list_1, @list_2);
print "@output";
 
Hey rharsh thanks for the comment, i tried that, and it prints out "1". I solved it by using the join() function
Code:
$final_list = join( ',',@list_1,@list_2);

#Where the first parameter in join is the delimter.
 
the interesting this, now after i do that it removes the '',s so
Code:
print $final_list;

gives me "my dog is lazy"

however the output im looking for is

'my','dog','is','lazy'
 
In your original example, you put the contents of list_1 and list_2. In your last example, it appears you're using a scalar. Which are you trying to use? Another question is, do you actually want the single quotes around all your strings?
 
Code:
foreach(@list1) {
 push @list3, $_;
}
foreach(@list2) {
 push @list3, $_;
}

There's gotta be an easier way to concat arrays, but this should do till you find it

--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Yes I would like to have all the quotes around the values. I put the results into a scalar because it has to be in that format when i pass it into another fucntion
 
Nevermind what i said before, i don't need the quotes... case closed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top