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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to form a string(no spaces) from the elements of an array?

Status
Not open for further replies.

aNewBeginning

Programmer
Aug 23, 2008
1
Suppose I wish to print 1234567890 x 3, and I wish to implement it in a reasonable way as follows:

my @count_to_ten = 0..9;
my $number_ruler = @count_to_ten x 3;
#number_ruler is "101010"
or
my $number_ruler = "@count_to_ten" x3;
#number_ruler is 0 1 2 3 4 5 6 7 8 90 1 .....

Is it possible to store a string of the variables from an array (without spaces) into a scalar?
 
$var = join(@array, '');



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Is it possible to store a string of the variables from an array (without spaces) into a scalar?

Travis had it backwards:

Code:
$var = join('',@array);

or:

Code:
$var = do{local $"; "@array";};

$" is the predefined list seperator variable.

Not sure if one way is better than another.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I prefer the former. I can see straight away that I'm joining the array with a '' separator, without having to remember what $" is...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top