news <[EMAIL PROTECTED]> wrote:
: 
:         I want to make a script which converts like 
: (pErl1234test = perl).I
: wrote like
: 
:         #!/usr/bin/perl

    Always use the following two statements at the
beginning of your scripts. They will help catch errors.

use strict;
use warnings;


:         print "Enter ur name"
:         $name = <STDIN>
:         $org_name = $name

    Almost every statement in perl should end with a
semicolon. The 'my' is added the first time you use a
variable or sometimes just before.

print 'Enter your name';
my $name         = <STDIN>;
my $org_name = $name;


:         $name =~ s/\W.*//;      #change 1

    \W will match any character that is /not/
alphanumeric. You can use a POSIX character class
([:alpha:]) for alphabetic only characters. It must be
place in a perl character class which is a set of square
brackets ([]). Read 'perlre'.


    Substitution is probably not the best way to handle
this. We'll come back in a bit.


:         $name =~ tr/A-Z/a-z/;  #change  2

    Perl has a lowercase function (lc) built-in. Read
'perlfunc'


:         print "Old = $org_name\n";
:         print "New = $name\n";


    Your solution was not built to handle '12345' or
'    ' or other inputs which might fail the conversion
process. Here's the solution I came up with:

#!/usr/bin/perl

use strict;
use warnings;

print 'Enter your name';

# we use my the first time we use a variable.
my $name = <STDIN>;

# We declare $new_name outside the if block
#    so we can use again later'
# We set it to '' in case the regex below fails
my $new_name = '';

# [:apha:] matches alphabetical characters only.
# /x at the end allows us to insert whitespace in
# the regex.
if ( $name =~ / ( [[:alpha:]]+ ) /x ) {

    # lc is the lower case function for perl
    # $1 is anything found in the parenthesis
    # in the regex above
    $new_name = lc $1;

}

print "Old = $name\n";
print "New = $new_name\n";

__END__


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to