Re: Concatenate printing a string and array
by CountZero (Bishop) on Jan 05, 2009 at 06:00 UTC
|
The concatenation "." (dot) operator provides a scalar context to its arguments. The value of an array in scalar context is the length of the array. As your array has 4 elements, its return value in scalar context is 4.Double quotes maintain the array context, so you could try the following: {
local $" = undef;
print "Your result is: @array";
}
Note the accolades to restrict the change to the $" (list separator variable) to the smallest scope possible.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
|
|
local $" = undef;
This produces warnings.
>perl -wMstrict -le
"my @ra = (1,2,3,4,5);
{ $\" = undef; print qq{@ra}; }
"
Use of uninitialized value in join or string at -e line 1.
12345
Try something like assigning $" the empty string instead of undef:
>perl -wMstrict -le
"my @ra = (1,2,3,4,5);
{ $\" = ''; print qq{@ra}; }
"
12345
Or better yet, just use join('', @ra) as suggested by others. | [reply] [d/l] [select] |
|
|
$ perl -Mstrict -wle '
> my @arr = ( 1, 2, 3 );
> print qq{@arr};
> { $" = q{}; print qq{@arr} }
> print qq{@arr};'
1 2 3
123
123
$
I usually limit the scope to the print itself by using a do BLOCK.
$ perl -Mstrict -wle '
my @arr = ( 1, 2, 3 );
> print do{ local $" = q{}; qq{@arr} };'
123
$
I hope this is of interest.
Cheers, JohnGG | [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
Re: Concatenate printing a string and array
by marcussen (Pilgrim) on Jan 05, 2009 at 05:10 UTC
|
You might want join
print join("",@array);
I think you might have misunderstood how interpolation and scalar versus list context works. However I would prefer to leave the explanation up to someone who is better at expressing their wisdom than myself.
Confucius says kill mosquito unless cannon
| [reply] [d/l] |
Re: Concatenate printing a string and array
by jethro (Monsignor) on Jan 05, 2009 at 06:12 UTC
|
Was that on purpose or a typo that you have a period (.) instead of a comma (,) between the string and your @array ?
If the former, you should read up about context. The concatenation operator (.) joins two scalars, so the array is in scalar context (i.e. the concatenation needs a scalar, so the array provides a scalar). An array provides its size in scalar context, in this case 4
The parameters of a print on the other hand (with the possible exception of the optional filehandle parameter) provide list context which is why a comma works
| [reply] |
|
|
print "The array is ", @array, "\n";
or
print( "The array is ", @array, "\n");
| [reply] |
|
|
print "The array is ", @array, "\n";
or
print( "The array is ", @array, "\n");
| [reply] [d/l] [select] |
Re: Concatenate printing a string and array
by velusamy (Monk) on Jan 05, 2009 at 05:44 UTC
|
use strict;
use warnings;
my @array=(1,1,1);
print "Reslut:",@array;
| [reply] [d/l] |
|
|
For the benefit of those whose eyesight isn't quite what it was, the difference between the original posting and this reply is that the dot/period has been replaced with a comma. :-)
(Oh, and "result" has been re-spelt more rudely!)
For the record, the same kind of approach can be expressed as:
print "Your result is: ";
print @array;
--
use JAPH;
print JAPH::asString();
| [reply] [d/l] |
Re: Concatenate printing a string and array
by imrags (Monk) on Jan 05, 2009 at 05:18 UTC
|
@array = (1,2,3,4,5);
local $"= "";
print "Your result is @array";
I am not sure how using output list separator ($") is going to affect the rest of your program
That is why i've put it local (assuming u r using a subroutine).
| [reply] [d/l] |
|
|
{
local $" = q{};
print qq{@array};
}
or a do BLOCK
print do{ local $" = q{}; qq{@array} };
I hope this is useful.
Cheers, JohnGG | [reply] [d/l] [select] |
Re: Concatenate printing a string and array
by Bass-Fighter (Beadle) on Jan 06, 2009 at 08:43 UTC
|
my $n = @array;
my $count=0;
for (my $i=0; $i<$n; $i++){
print $array[$count];
$count++
}
$n gets in here the number of binary numbers
and $count gives you which number in the array must be printed | [reply] [d/l] |