--On Tuesday, October 14, 2003 15:59 -0700 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

Can someone shorten this upper routine?

sub toUpper
{ my $z = shift;
 $z =~ tr/a-z/A-Z/;
 return $z;
}

Sure: sub toUpper { $_[0] =~ tr/a-z/A-Z/; } or sub toUpper { "\U$_[0]"; }

I wouldn't write a function for this myself, I'd just use the uppercase shortcut to oneline it:

$var = "\U$var";

Also in a slightly different scenario, how can i change the value
of the parameter itself with a sub.

$a="ca";
toUpper($a);  #change the $a value itself
print $a;      #I want it to print "CA"

Ok, I'll assume this is a made-up example... (Since it would just be easy to write 'print toUpper($a);', using the return value.)


The way to do this would be to pass references to the parameters. You would have to pass the references, and work with them. So, the function call would become:

toUpper(\$a);

The sub would be:

sub toUpper { return "\U$$_[0]"; } # (Hey, we have linenoise!)

In general, this is to be avoided, just because it makes things more complicated than they really need to be (and it breeds linenoise). There is little you can do this way that you can't with return values anyway.

Also, may I suggest picking up a copy of "Learning Perl"? It's a short book, available in all the better bookstores, and goes over most of these questions in an easy to read and use manner.

Daniel T. Staal

---------------------------------------------------------------
This email copyright the author.  Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes.  This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to