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 ]


In reply to Re: String concatenation function by halley
in thread String concatenation function by jonnyr9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.