[Please do not top post, trying to repair]
[Please do not blindly fullquote]
Jürgen Exner napisal(a):
But for me '\0' is the mark of the end of the array
In Perl the 'end' of an array is managed internally and is accessible to the
programmer in two different ways:
- $#array indicates the last used index in the array @array
- scalar(@array) indicates the number of elements in the array (usually
$#array+1 unless someone messed around with the start index of the array)
(or string we can say when the type is char).
An array of char has nothing to do with a string. Those are two totally
disjunct and unrelated data structures.
So when I have >>>$string = "abcd"<<< is there somewhere '\0' in the
end?
No, why should there be? Opposite to C in Perl that horrible crutch is not
needed. If you need the length of an array then use one of the two methods
mentioned above. If you need the length of a string then just use the
length() function.
Note: of course the first has nothing, nothing at all, to do with the
second.
Would you mind fixing your keyboard?
Ex.
char a[10] = "abcd";
and then we have
a[0] = 'a' a[1]='b' a[2]='c' a[3]='d'
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
So this is what I am talking about.
Yes, it is amazing how primitive C is when it comes to data structures. It
doesn't even know about strings and forces the programmer not only to use an
array of characters instead, but to manually(!) manage even the length of
the string. And if you are not very careful you can screw up really badly,
e.g. char a[10] = "abcdabcdabcdabcd";
Luckily Perl is far advanced in this regard:
$a = 'abcd';
is all you need to declare and define a string that contains the first 4
latin letters. And if you want to enlarge the string, then just do so:
$a = $a . 'abcdabcsabcdabcd'.
There may or may not be an array @a, too, but it would be totally unrelated
to the scalar $a.
jue