arrays and scalars.

R

radioactiveman

hi,i'm new to perl and programming but is there a way to put contents in
an array like @colors=("blue","red","green); into a scalar where
$allcolors=blueredgreen?
 
K

Koms Bomb

hi,i'm new to perl and programming but is there a way to put contents in
an array like @colors=("blue","red","green); into a scalar where
$allcolors=blueredgreen?

You should have a look at function 'join' in the Perl document.
It's what you need.
And you'd better study some functions that operate on array/list, eg, map,
grep,
 
T

Tore Aursand

hi,i'm new to perl and programming but is there a way to put contents in
an array like @colors=("blue","red","green); into a scalar where
$allcolors=blueredgreen?

It looks like you want to read more about 'join';

perldoc -f join

Example:

my @array = qw( blue red green );
my $scalar = join( '', @array );
 
R

radioactiveman

thanks everybody. i've read most of the array-list functions but
somehow missed this join function. all is good.
 
R

Robin

{
local ($", $patriot);
$patriot = "@Patriot";
print $patriot;
}

what's this do? Without defining your subroutine, having it be the main
block...is it kinda like (correct me if I'm wrong), the "main" function in
c++?
Thanks.
-Robin
 
J

Joe Smith

Robin said:
what's this do?

The braces define the scope of local(). Once the flow of control reaches
the closing brace, the previous value of $" is restored.
-Joe
 
T

Tad McClellan

Copies the current values of those variables into a "secret place",
and puts a value of undef into each of them.



Stomps over the undef with a string that is the concatenation
of all of the elements in the array.



Outputs the value of the variable.



Copies the values saved earlier in the secret place, back into $" and $patriot.

what's this do?


The same as all of these:

print @Patriot;
or
print join '', @Patriot;
or
print for @Patriot;

only harder to read and understand.


Where did you get that code?

It is of poor quality, which may say something about the rest of
the program that that snippet came from...

Without defining your subroutine,


What "subroutine"?

There is no subroutine there.

There is a BLOCK there.

You can have a BLOCK in many places besides the definition of a subroutine.

having it be the main
block...


See the "Basic BLOCKs and Switch Statements" section of:

perldoc perlsyn

is it kinda like (correct me if I'm wrong), the "main" function in
c++?


No.

The "main" in Perl is all of the code that is not in a subroutine definition.

There is also the "main" package, but that has to do with variables
rather than with execution of the program.

The poor code you've shown is what is usually called a "naked block",
which is often used to control the scope of a variable or its value.
 
B

Brian McCauley

Tad McClellan said:
Copies the current values of those variables into a "secret place",
and puts a value of undef into each of them.

This is a widely repleted myth. Indeed for a long time I perpeuated
this myth myself but actually local($patriot) does not save (and later
restore) the value of $patriot. It actually saves and restores the
value of *patriot{SCALAR} (the scalar slot within he patriot GLOB).

our $patriot ="Outer";
my $r = \$patriot;
print "$patriot $$r\n";
{
local $patriot = "Inner";
print "$patriot $$r\n";
}
print "$patriot $$r\n";
__END__
Outer Outer
Inner Outer
Outer Outer

Compare this with saving and restoring the value of $patriot:

our $patriot ="Outer";
my $r = \$patriot;
print "$patriot $$r\n";
{
my $save = $patriot;
require AtExit;
my $restore = AtExit->new(sub{$patriot = $save});
$patriot = "Inner";
print "$patriot $$r\n";
}
print "$patriot $$r\n";
__END__
Outer Outer
Inner Inner
Outer Outer


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top