CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Appending to Variable

 



abockover
Deleted

Dec 27, 2000, 7:45 PM

Post #1 of 5 (962 views)
Appending to Variable Can't Post

How can I append to a variable? such as:

==========BGN==========

$error = "Your Errors: \n";

if($this eq "that")
{
$error =~ "ERROR 1\n";
}
if($that eq "this")
{
$error =~ "ERROR 2\n";
}

print $error;

==========END==========

I have tried something like this, but it doesn't work, the only thing that gets printed out is the initial $error (in the case of the example above, the initial $error = "Your Errors\n";).

What am I doing wrong?

Thank you,

-Aaron



randor
User

Dec 28, 2000, 5:27 AM

Post #2 of 5 (958 views)
Re: Appending to Variable [In reply to] Can't Post

abockover,

from what i see on your example, the problem you are causing is that =~ is a comparison operator, not an assignment operator. try this:

$error = "Your Errors: \n";

if ($this eq "that") { $error = "ERROR 1\n"; }
elsif ($that eq "this") { $error = "ERROR 2\n"; }
print $error;

i hope this helps..

Randor



abockover
Deleted

Dec 28, 2000, 6:15 AM

Post #3 of 5 (957 views)
Re: Appending to Variable [In reply to] Can't Post

The problem with your example [randor] is that the $error will not contain a list of errors, or, lets say that:

if($this eq "that")
{
$error = "ERROR 1\n";
}

next in line, we check for:

if($that eq "this")
{
$error = "ERROR 2\n";
}

since the second if() statement was found to be true, $error was overwritten and thus its previous value was erased from the first passing of the if() statement above.

My goal is to collect a list of errors in one variable, $error.

Your example [randor] overwrites previous values.

Thanks anyway,

Aaron



japhy
Enthusiast

Dec 28, 2000, 7:33 AM

Post #4 of 5 (955 views)
Re: Appending to Variable [In reply to] Can't Post

There are a couple ways to append:


Code
$foo = "jeff"; 
$foo .= "rey"; # "jeffrey"

$foo = "pin";
$foo = "${foo}yan"; # "pinyan"

The {...} are needed around foo there because otherwise, Perl would think I was using the variable $fooyan.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


abockover
Deleted

Dec 28, 2000, 10:15 AM

Post #5 of 5 (951 views)
Re: Appending to Variable [In reply to] Can't Post

japhy,

thank you, that is just what I was looking for.
makes perfect sense.

-Aaron


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives