For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > grep help request









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author grep help request
David Gilden

2005-10-28, 6:56 pm

Hello,

I am stuck with this issue.
How do get each substring (the names in this case) and then upper case them=
=2E
This does not work just yet....

Possible data:
mike smith
john h. hamilton=20
g. bush
hendric

etc......
----

#!/usr/bin/perl=20

$SendersName =3D " dave middlename gilden ";
$SendersName =3D~ s/\s*(.+)\s*/$1/g;
($SendersName) =3D $SendersName =3D~ m/([\w\.-]{1,24}( [\w\.-]{1,24}){0,4})=
/;
$SendersName =3D~ s/.+ (.+)? (.+){1,3}/\u$1 \u$2 \u$3/g;
print "$SendersName\n";


Thanks!

Dave Gilden -- Ft. Worth

Island Journey music video now online!
<http://www.abbydigital.tk/>

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Cora Connection: Your West African Music Source
Resources, Recordings, Instruments & More!
<http://www.coraconnection.com/>=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


Paul Lalli

2005-10-28, 6:56 pm

David Gilden wrote:
> I am stuck with this issue.
> How do get each substring (the names in this case) and then upper case them.
> This does not work just yet....
>
> Possible data:
> mike smith
> john h. hamilton
> g. bush
> hendric
>
> etc......
> ----


I'm curious as to what you believe each of these lines do...
> #!/usr/bin/perl
>
> $SendersName = " dave middlename gilden ";
> $SendersName =~ s/\s*(.+)\s*/$1/g;


Removing all spaces from the string. $SendersName is now, for example,
'johnh.hamilton'

> ($SendersName) = $SendersName =~ m/([\w\.-]{1,24}( [\w\.-]{1,24}){0,4})/;


matching between 1 and 5 instances of strings containing word
characters, periods, and dashes. Each instance separated by a space.
The entire sequence returned as the new $SendersName

> $SendersName =~ s/.+ (.+)? (.+){1,3}/\u$1 \u$2 \u$3/g;


Look for anything, a space, a possible anything, a space, and between
one and three anythings. Replace with a first-letter uppercased
version of the second string (which may not have existed), a
first-letter uppercased version of the last of the group of three
anythings, and a first-letter uppercased version of undefined.

What on earth is that trying to do?

> print "$SendersName\n";


#!/usr/bin/perl
use strict;
use warnings;

while (my $SendersName = <DATA> ){
chomp $SendersName;
(my $upper_cased = $SendersName) =~ s/\b(\w)/\u$1/g;
print "$SendersName ==> $upper_cased\n";
}

__DATA__
mike smith
john h. hamilton
g. bush
hendric

Output:
mike smith ==> Mike Smith
john h. hamilton ==> John H. Hamilton
g. bush ==> G. Bush
hendric ==> Hendric


Paul Lalli

John Doe

2005-10-28, 6:56 pm

David Gilden am Freitag, 28. Oktober 2005 20.55:
> Hello,
>
> I am stuck with this issue.
> How do get each substring (the names in this case) and then upper case
> them. This does not work just yet....
>
> Possible data:
> mike smith
> john h. hamilton
> g. bush
> hendric
>
> etc......
> ----
>
> #!/usr/bin/perl
>
> $SendersName = " dave middlename gilden ";
> $SendersName =~ s/\s*(.+)\s*/$1/g;
> ($SendersName) = $SendersName =~ m/([\w\.-]{1,24}( [\w\.-]{1,24}){0,4})/;
> $SendersName =~ s/.+ (.+)? (.+){1,3}/\u$1 \u$2 \u$3/g;
> print "$SendersName\n";


use strict;
use warnings;

my @names=('mike smith', 'john h. hamilton', 'g. bush', 'hendric');

for (@names) {
print join ' ', map ucfirst, split /\s+/;
print "\n";
}

Mike Smith
John H. Hamilton
G. Bush
Hendric


joe

>
>
> Thanks!
>
> Dave Gilden -- Ft. Worth
>
> Island Journey music video now online!
> <http://www.abbydigital.tk/>
>
> ========================================
========
> Cora Connection: Your West African Music Source
> Resources, Recordings, Instruments & More!
> <http://www.coraconnection.com/>
> ========================================
========

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com