Solved

Declare a long string using $

Posted on 2006-11-17
4
855 Views
Last Modified: 2011-10-03
Hi,

I'm new in Perl. I've an assignment to edit perl script.

This is the original line
my $MAILRCP = 'john,anna,steven';

If I have a very long string(additional name to add on) , can I do like this:
my $MAILRCP = 'john,anna,steven,garry,tom,Jeff',
                        'alia','mike','guna','larry';

Please help.
Thanks
0
Comment
Question by:azsoft
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17963989
You can do it like this:
my $MAILRCP = 'john,anna,steven,garry,tom,Jeff'.
                        'alia'.'mike'.'guna'.'larry';
or like this:
my $MAILRCP = 'john,anna,steven,garry,tom,Jeff
                        alia,mike,guna,larry';
or like
my $MAILRCP = join",",'john,anna,steven,garry,tom,Jeff',
                        'alia','mike','guna','larry';
Depending on what you want the string to contain
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 17967325
to append to the end of a string you use the concatenation operator '.=' but it all depends on the structure of the data you are adding. If the string to add is already formatted exactly how you need it:

$foo = 'this';
$bar = 'that';
$foo .= $bar;
print $foo;


as ozo showed above there are numerous ways to append data to a string.


0
 
LVL 3

Accepted Solution

by:
Lil earned 50 total points
ID: 17970504
my $MAILRCP = "john, anna, steven";
#to add a new name to this:
$newnames = "garry, tom, Jeff, alia, mike, guna, larry";
$MAILRCP .= ", ".$newnames;

If this was a subroutine, it would look like this:

sub addNames()
{
my $MAILRCP = "john, anna, steven";
my $newnames = "garry, tom, Jef, alia, mike, guna, larry";
$MAILRCP .= ", ".$newnames
 return $MAILRCP;
}
0
 
LVL 1

Expert Comment

by:mallah
ID: 17973842


>If I have a very long string(additional name to add on) , can I do like this:

if you have a number of additional names to add on from some source say from a file its
better to loop and process one record at a time.

putting all the names in one var will take lots of memory.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question