in reply to String concatenation function

Okay, the others have already stated that there's an operator which does what your function does. That's all a part of learning. And it is clear that you're just learing Perl. Welcome. In that vein, with no offense intended, let me go through your script with some other comments.
############################### # STRING CONCAT FUNCTION # # BY DR JONATHAN REES # ###############################
I find "box comments" to be a waste of time. The all-capitals also are harder to read than plain English... this looks like some old FORTRAN-66 code.
$l = 5; # $l defines the number of asterisks
You probably shouldn't assign to $1, $2, or so on. These special variables receive the results of regular expression captures. But maybe that's a lowercase 'ell' letter. You might avoid single letter variables too, especially ones that look like 0 O 1 l...
&concat($l); # calls the sub concat passing the # number of concatenations in $l
Pick your syntax: either &concat or concat(...). Combining them is redundant and may confuse someone maintaining your code.

Also, don't describe the effect of every statement with a comment. Comments are not to explain what the statement does, they're to explain what the algorithm does. A year from now when you're very quick with Perl, the transliteral comments will just be distracting noise. Put strategy in comments, tactics in code.

print $i; # prints the result
Another example of transliteral comments. And where did $i come from? It would seem much more natural to have the function return a value and for the caller to catch the value in whatever variable they choose.

And try to end your last printout with a newline. That's either the default value of $/, or a literal "\n".

sub concat { ... }
Pick an indentation style that helps people read your code. If the closing curly brace was in column 1, people could see it was related to the sub statement.
my $val = $_[0] + 1; for ($k = 1 ; $k < $val ; $k++){ $i = $i.$sym; }
Firstly, you seem to assume that $i will be empty when you call the function... what happens when you call this function twice?

Secondly, Perl numbers its arrays from zero, as you've discovered here. But you seem to think of iteration as starting with one. This isn't a crime, but you seem to have stumbled upon a loop strategy that gets you the right number of iterations, quite accidentally. Since the number in $k isn't useful to the loop, I'd suggest:

my $i = ''; for (1 .. $_[0]) { $i = $i . $sym }
This leaves us with the following code (assuming there was no such  'abc' x 5 operation):
# Concatenate a number of symbols. print concat('abc', 5), $/; sub concat { my $i = ''; for (1 .. $_[1]) { $i = $i . $_[0] } return $i; }

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: String concatenation function
by Juerd (Abbot) on Jun 02, 2003 at 05:20 UTC

    You probably shouldn't assign to $1, $2, or so on.

    You can't. This is indeed lowercase L.

    You might avoid single letter variables too, especially ones that look like 0 O 1 l...

    And you might want to use a font that doesn't render different things characters the same.

    Pick your syntax: either &concat or concat(...). Combining them is redundant and may confuse someone maintaining your code.

    &foo without parens and arguments is scary (implicitly passes @_). This is *exactly* why you shouldn't use the &-form unless you know what you are doing. Style is not important here.

    This leaves us with the following code (assuming there was no such 'abc' x 5 operation):

    One direct access of an @_ element is okay, but once you start using more, in my opinion you should assign the arguments to variables. And still, $i .= $foo is clearer and less work than $i = $i . $foo;

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Thanks for all your help! Learning fast...