Re: Strings vs Arrays
- From: Alan_C <mtbr0228AT@xxxxxxxxxxxxxxxx>
- Date: Sat, 30 Dec 2006 09:37:23 GMT
cajun@xxxxxxxxxxxx (M. Lewis) writes:
I now we can push (concatenate) data onto an array.
It's *probably* thought of more often as *adding* an item/element onto an
array (rather than as concatenate). In array we are not merging nor are we
making the all as merged/fused into just one unit. Instead we are increasing
the number of elements in the array. Each element of the array remains as it
was, nothing gets merged/fused together as one.
I would assume that we could concatenate data on to a string as well
with something like:
$newstring = $oldstring . $newdata
Maybe this isn't correct though. I've not yet tried it.
My question is one method 'better' than the other? If so, why so?
Assuming there is no difference, then perhaps the determination of
which to use is dependent entirely on what is to be done with the data
later in the program.
As DJ offered a hint. As in use whichever tool you need so as to accomplish
whichever specific task is at hand.
An array may contain multiple strings/elements. Array can be iterated over.
String is just one string; there's nothing to loop or iterate over.
And, *why* haven't you tried it yet? The term for it is hands on or
kinisthetic learning.
The next is tested, works.
#!/usr/bin/perl
use warnings;
use strict;
my $oldstring = 'string_item_1';
my $newdata = 'string_item_2';
print $oldstring, " \<\< \$oldstring\n";
print $newdata, " \<\< \$newdata\n";
# my $newstring = '$oldstring . $newdata';
# my $newstring = "$oldstring . $newdata . string_item_3";
my $newstring = "$oldstring" . " $newdata" . " string_item_3";
print "$newstring", " \<\< \$newstring\n";
# foreach ( $newstring ) {}# oop won't work
my @array = qw(Joe Larry Moe);
foreach (@array ) { # works
print $_, " \<\< comical person\n";
}
print "The listed comics may not be mathematicians nor rocket scientists\n";
# end
--
Alan.
.
- References:
- Strings vs Arrays
- From: M. Lewis
- Strings vs Arrays
- Prev by Date: Re: Strings vs Arrays
- Next by Date: Re: Deleting files remotely
- Previous by thread: Re: Strings vs Arrays
- Next by thread: [bug or restriction about msgget] I have a confusion about msgget(System V message queue)
- Index(es):
Relevant Pages
|
|