- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

There are various ways to print a perl array.
You can use a loop like this:


for( $i = 0; $i < @names; ++$i ) {
print $names[$i]." ";
}


Not a really good. How about using foreach loop?

foreach $i (@names) {
print $i." ";
}


Look better, right? But I've found a little interesting for.


for $i (@names) {
print $i." ";
}


It's still good!
How about some macro? Try this one


for (@names) {
print;
}


Uhm...but each element is not spaced out from each other..then, just make it space out


for (@names) {
print $_." ";
}


How about not using a loop? is there any way?
Oh, yeah...a great idea


print @names;


Oooops, elements are sticky together..space them out!


print "@names ";


It's better!
But I want to comma between each element, how?
In Perl, there is one special variable the set default output field seperator $,. Let's do this!


$, = ',';
print @names;


Wow! this is wonderful!
Is there any other option for printing an array?
You can join them before displaying.


print join( ',', @names);


This is a really nice trick!

Have fun!@

0 comments

Post a Comment