| Author |
string change problem
|
|
| Muthukumar 2004-03-29, 6:30 am |
| Hai all.
I want to make a script which converts like (pErl1234test = perl).I
wrote like
#!/usr/bin/perl
print "Enter ur name"
$name = <STDIN>
$org_name = $name
$name =~ s/\W.*//; #change 1
$name =~ tr/A-Z/a-z/; #change 2
print "Old = $org_name\n";
print "New = $name\n";
But i can not get a change on the change 1 and 2 lines.
Regards,
Muthukumar.
| |
| Smoot Carl-Mitchell 2004-03-29, 1:35 pm |
| On Mon, 29 Mar 2004 15:05:34 +0530
"MuthuKumar" <ramana_muthu@yahoo.co.in> wrote:
> Hai all.
>
> I want to make a script which converts like (pErl1234test =
> perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
> $name =~ s/\W.*//; #change 1
This RE deletes zero or more non-alphanumeric characters followed by
anything.
So it deletes the entire string, since there was no match on a
non-alphanumeric character.
You probably meant something like:
$name =~ s/\d+.*//;
which matches one or more digit characters followed by anything.
See the perlre man page for details on Perl regular expressions.
--
Smoot Carl-Mitchell
Systems/Network Architect
email: smoot@tic.com
cell: +1 602 421 9005
home: +1 480 922 7313
| |
| James Edward Gray II 2004-03-29, 2:33 pm |
| On Mar 29, 2004, at 3:35 AM, MuthuKumar wrote:
> Hai all.
Howdy.
> I want to make a script which converts like (pErl1234test =
> perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
You are missing semi-colons on all three lines above.
> $name =~ s/\W.*//; #change 1
In English, that says, "Find a non-word character followed by zero or
more anything characters and delete them." Perl's idea of non-word
characters is anything not in the set [A-Za-z0-9_]. You don't have any
non-word characters in your sample string, so it does not match.
I believe you wanted:
$name =~ s/[^A-Za-z].*//;
> $name =~ tr/A-Z/a-z/; #change 2
The above line looks like it should be lowercasing everything to me,
though I would probably write it as:
$name = lc $name;
Hope that helps.
James
> print "Old = $org_name\n";
> print "New = $name\n";
>
> But i can not get a change on the change 1 and 2 lines.
>
> Regards,
> Muthukumar.
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Flemming Greve Skovengaard 2004-03-29, 2:33 pm |
| MuthuKumar wrote:
> Hai all.
>
> I want to make a script which converts like (pErl1234test = perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
> $name =~ s/\W.*//; #change 1
> $name =~ tr/A-Z/a-z/; #change 2
> print "Old = $org_name\n";
> print "New = $name\n";
>
> But i can not get a change on the change 1 and 2 lines.
>
> Regards,
> Muthukumar.
>
>
>
>
This should work:
__BEGIN__
#!/usr/bin/perl
my ($name, $org_name);
print "Enter your name: ";
chomp($name = <STDIN> );
$org_name = $name;
$name =~ s/[^A-Za-z]\w*//g; #change 1
$name =~ tr/A-Z/a-z/; #change 2
print "Old = $org_name\n";
print "New = $name\n";
__END__
--
Flemming Greve Skovengaard And when you kill a man, you're a murderer
a.k.a Greven, TuxPower Kill many, and you're a conqueror
<dsl58893@vip.cybercity.dk> Kill them all ... Ooh ... Oh you're a god!
4168.08 BogoMIPS - MegaDeth, Countdown to Extinction
| |
| Wc -Sx- Jones 2004-03-29, 2:34 pm |
| MuthuKumar wrote:
> Hai all.
>
> I want to make a script which converts like (pErl1234test = perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
> $name =~ s/\W.*//; #change 1
> $name =~ tr/A-Z/a-z/; #change 2
> print "Old = $org_name\n";
> print "New = $name\n";
>
#! /usr/bin/perl -w
print "Enter ur name";
my $name = <STDIN>;
my $org_name = $name;
$org_name =~ s/[^a-z]/ /gi;
$org_name =~ tr/A-Z/a-z/;
print " Org = $org_name\n";
print "As Entered = $name\n";
HTH/Sx
| |
| Randy W. Sims 2004-03-29, 2:34 pm |
| MuthuKumar wrote:
> Hai all.
>
> I want to make a script which converts like (pErl1234test = perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
> $name =~ s/\W.*//; #change 1
> $name =~ tr/A-Z/a-z/; #change 2
> print "Old = $org_name\n";
> print "New = $name\n";
>
> But i can not get a change on the change 1 and 2 lines.
The problem with 'change 1' is that word characters (\w) _include_
digits where you seem to expect that they don't, so I use a character
class to produce the results that you /seem/ to want.
#!/usr/bin/perl
use strict;
use warnings;
print 'Enter your name: ';
my $new_name = <STDIN>;
chomp($new_name);
my $old_name = $new_name;
$new_name =~ s/[\W\d].*$//; #change 1
$new_name =~ tr/A-Z/a-z/; #change 2
print "Old = $old_name\n";
print "New = $new_name\n";
__END__
Regards,
Randy.
| |
| John W. Krahn 2004-03-29, 2:36 pm |
| Muthukumar wrote:
>
> Hai all.
Hello,
> I want to make a script which converts like (pErl1234test = perl).I
> wrote like
>
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
> $name =~ s/\W.*//; #change 1
The \W character class includes every character that is NOT a-z and A-Z
and 0-9 and _. If you just want to keep only the letters at the
beginning of the string:
$name =~ s/[^a-zA-Z].*//;
Or:
$name =~ s/[^[:alpha:}].*//;
Or:
$name = $1 if $org_name =~ /^([a-zA-Z]+)/;
etc.
> $name =~ tr/A-Z/a-z/; #change 2
You can also use the lc() function for that.
$name = lc $name;
> print "Old = $org_name\n";
> print "New = $name\n";
>
> But i can not get a change on the change 1 and 2 lines.
John
--
use Perl;
program
fulfillment
| |
| Smoot Carl-Mitchell 2004-03-29, 2:36 pm |
| On Mon, 29 Mar 2004 12:14:49 -0600
James Edward Gray II <james@grayproductions.net> wrote:
> On Mar 29, 2004, at 11:02 AM, Smoot Carl-Mitchell wrote:
>
>
> Half-right. ;) It deletes nothing, since there was no match...
Oops. You are correct. '\W' matches exactly once. I was thinking
'\W*'.
--
Smoot Carl-Mitchell
Systems/Network Architect
email: smoot@tic.com
cell: +1 602 421 9005
home: +1 480 922 7313
| |
| Charles K. Clarkson 2004-03-29, 2:36 pm |
| news <news@sea.gmane.org> 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
| |
| Jeff 'Japhy' Pinyan 2004-03-29, 3:33 pm |
| On Mar 29, MuthuKumar said:
> I want to make a script which converts like (pErl1234test = perl).I
> #!/usr/bin/perl
> print "Enter ur name"
> $name = <STDIN>
> $org_name = $name
Those three lines are all missing semicolons.
> $name =~ s/\W.*//; #change 1
> $name =~ tr/A-Z/a-z/; #change 2
Those should work, but I'd use lc() instead of tr/A-Z/a-z/.
> print "Old = $org_name\n";
> print "New = $name\n";
#!/usr/bin/perl -w
use strict; # look into this -- it enforces safer coding practices
print "Enter your name: ";
chomp(my $name = <STDIN> ); # chomp removes the ending newline
my $old_name = $name;
$name =~ s/\W.*//;
$name = lc $name;
print "Old = $old_name\n";
print "New = $name\n";
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
| |
| James Edward Gray II 2004-03-29, 3:33 pm |
| On Mar 29, 2004, at 11:02 AM, Smoot Carl-Mitchell wrote:
> On Mon, 29 Mar 2004 15:05:34 +0530
> "MuthuKumar" <ramana_muthu@yahoo.co.in> wrote:
>
>
> This RE deletes zero or more non-alphanumeric characters followed by
> anything.
>
> So it deletes the entire string, since there was no match on a
> non-alphanumeric character.
Half-right. ;) It deletes nothing, since there was no match...
James
| |
| Charles K. Clarkson 2004-03-29, 5:31 pm |
| WC -Sx- Jones <sx@insecurity.org> wrote:
:
: MuthuKumar wrote:
: >
: > I want to make a script which converts
: > like (pErl1234test = perl). I wrote like
:
: #! /usr/bin/perl -w
:
: print "Enter ur name";
: my $name = <STDIN>;
: my $org_name = $name;
: $org_name =~ s/[^a-z]/ /gi;
: $org_name =~ tr/A-Z/a-z/;
: print " Org = $org_name\n";
: print "As Entered = $name\n";
Sorry, Sx. That produces this:
Org = perl test
As Entered = pErl1234test
The OP wanted:
Org = perl
As Entered = pErl1234test
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Wc -Sx- Jones 2004-03-29, 5:31 pm |
| Charles K. Clarkson wrote:
> WC -Sx- Jones <sx@insecurity.org> wrote:
> :
> : MuthuKumar wrote:
> : print "Enter ur name";
> Sorry, Sx. That produces this:
>
> Org = perl test
> As Entered = pErl1234test
>
>
> The OP wanted:
>
> Org = perl
> As Entered = pErl1234test
Sure, but "Enter ur name" could mean anything.
There were so many errors in the orginal post I was left with artistic
freedom.
-Sx-
| |
| Muthukumar 2004-03-30, 4:38 am |
| Hello All,
I am really very happy to receive the detailful response from you all.It is great.
Thanks for all helping me to start my knowledge on PERL programming.
Thanks,
Muthukumar.
----- Original Message -----
From: "Charles K. Clarkson" <cclarkson@htcomp.net>
To: "'MuthuKumar'" <ramana_muthu@yahoo.co.in>; <beginners@perl.org>
Sent: Monday, March 29, 2004 6:09 PM
Subject: RE: string change problem
> news <news@sea.gmane.org> 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
| |
| Wiggins D Anconia 2004-03-30, 10:36 am |
| Please bottom post.
> Hello All,
>
> I am really very happy to receive the detailful response
from you all.It is great.
> Thanks for all helping me to start my knowledge on PERL programming.
>
'perl' or 'Perl', never 'PERL'.
perldoc -q '"Perl"'
http://danconia.org
|
|
|
|