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:
pattern matching

 



john8320
New User

Dec 8, 2003, 7:56 AM

Post #1 of 24 (4837 views)
pattern matching Can't Post

I need a simple pattern matching/substitution/translation statement which will do the following:
all below are valid (case sensative)
John
John,Jodie
John,Jodie,Susan
I need a way of returning

$result = $name =~s/[A-Z] .........../

where Result ends up as follows

J.
J. J.
J. J. S.


John


KevinR
Veteran


Dec 8, 2003, 9:00 PM

Post #2 of 24 (4834 views)
Re: [john8320] pattern matching [In reply to] Can't Post

one way:


Code
#!/perl/bin/perl.exe -w 
use strict;
my @final_array;
print qq~Content-type: text/html\n\n~;

while ( <DATA> ) {#assumes your file is already open
chomp;
my @name_array = split(/\,/,$_);
$_ = substr $_,0,1 for @name_array;
push @final_array, join('.',@name_array);
}
print "$_<br>\n" for @final_array;

__DATA__
John
John,Jodie
John,Jodie,Susan
John,Jodie,Susan,Frank
John,Jodie,Susan,Frank,Mary

-------------------------------------------------


KevinR
Veteran


Dec 8, 2003, 9:20 PM

Post #3 of 24 (4833 views)
Re: [john8320] pattern matching [In reply to] Can't Post

if you prefer just using a regexp:

Code
#!/perl/bin/perl.exe -w 
use strict;
my @final_array;
print qq~Content-type: text/html\n\n~;

while ( <DATA> ) {#assumes file is already open
chomp;
1 while $_ =~ s/,?([A-Z])[a-z]+/$1\./g;
push @final_array, $_;
}
print "$_<br>\n" for @final_array;

__DATA__
John
John,Jodie
John,Jodie,Susan
John,Jodie,Susan,Frank
John,Jodie,Susan,Frank,Mary

-------------------------------------------------


(This post was edited by KevinR on Dec 8, 2003, 9:23 PM)


uri
Thaumaturge

Dec 8, 2003, 10:08 PM

Post #4 of 24 (4830 views)
Re: [KevinR] pattern matching [In reply to] Can't Post


Code
perl -lne 'print map "$_.", /([A-Z])[a-z]*/g'



Recall
Novice

Dec 9, 2003, 2:25 AM

Post #5 of 24 (4828 views)
Re: [uri] pattern matching [In reply to] Can't Post

It can be made even shorter =)


KevinR
Veteran


Dec 9, 2003, 1:18 PM

Post #6 of 24 (4822 views)
Re: [uri] pattern matching [In reply to] Can't Post


In Reply To

Code
perl -lne 'print map "$_.", /([A-Z])[a-z]*/g'



I don't see how that will generate a list like this:

J.
J. J.
J. J. S.

from the data file, it looks like it will do something like this:

J.J.J.J.J.S.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Dec 9, 2003, 2:23 PM

Post #7 of 24 (4821 views)
Re: [KevinR] pattern matching [In reply to] Can't Post


In Reply To

In Reply To

Code
perl -lne 'print map "$_.", /([A-Z])[a-z]*/g'



I don't see how that will generate a list like this:

J.
J. J.
J. J. S.

from the data file, it looks like it will do something like this:

J.J.J.J.J.S.


You should probably check what the -l command line option does.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Dec 9, 2003, 10:01 PM

Post #8 of 24 (4818 views)
Re: [davorg] pattern matching [In reply to] Can't Post


In Reply To
You should probably check what the -l command line option does.



Ah, thanks Dave, I will do that Blush
-------------------------------------------------


uri
Thaumaturge

Dec 10, 2003, 12:55 AM

Post #9 of 24 (4815 views)
Re: [Recall] pattern matching [In reply to] Can't Post


In Reply To
It can be made even shorter =)


i wasn't trying to start a golf game here. i was just showing a simple and fairly easy to understand way to do it. the other code i saw here was way too long and clumsy IMO.

and why don't you post your super short answer?

uri


Recall
Novice

Dec 10, 2003, 3:03 PM

Post #10 of 24 (4811 views)
Re: [uri] pattern matching [In reply to] Can't Post


Quote
i was just showing a simple and fairly easy to understand way to do it. the other code i saw here was way too long and clumsy IMO.



Its just a shame that your example was for the command line and not a script in which the -l switch can't be used Crazy


uri
Thaumaturge

Dec 10, 2003, 3:18 PM

Post #11 of 24 (4806 views)
Re: [Recall] pattern matching [In reply to] Can't Post


In Reply To
Its just a shame that your example was for the command line and not a script in which the -l switch can't be used Crazy


oh balderdash!!

i used a one liner to illustrate a point and -l to make it look cleaner. you mentioned you could make it shorter but haven't posted any code yet. i think thou doth protest too much.

here it is without the amazing -l:

Code
perl -ne 'print map( "$_.", /([A-Z])[a-z]*/g), "\n"'


and here it is in a script:


Code
#!/usr/local/bin/perl -n 
print map( "$_.", /([A-Z])[a-z]*/g), "\n"


did that make you happier?

no cookie or smiley for you!

one liners are great for testing and demonstrating snippets of perl code, especially string munging stuff. i use them all the time. maybe you should too.

uri


davorg
Thaumaturge / Moderator

Dec 11, 2003, 1:38 AM

Post #12 of 24 (4804 views)
Re: [Recall] pattern matching [In reply to] Can't Post


In Reply To
Its just a shame that your example was for the command line and not a script in which the -l switch can't be used


Why can't you use the -l switch in a script? I can think of at least two ways. You can either put the -l on the end of your shebang line or set the $\ directly (which is what -l does if you read the documentation).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Recall
Novice

Dec 11, 2003, 2:38 AM

Post #13 of 24 (4803 views)
Re: [davorg] pattern matching [In reply to] Can't Post


Quote
(which is what -l does if you read the documentation).



Thanks for the advice, but I'm aware of the specifics having programmed in perl for quite some time and written modulated scripts several megabytes in sizes consisting of many thousands of lines of strict, mod_perl compatible code, but I appreciate the sentiment Laugh


Recall
Novice

Dec 11, 2003, 2:38 AM

Post #14 of 24 (4802 views)
Re: [uri] pattern matching [In reply to] Can't Post


Quote
i use them all the time. maybe you should too.



Its never wise to assume =)


uri
Thaumaturge

Dec 11, 2003, 1:09 PM

Post #15 of 24 (4793 views)
Re: [Recall] pattern matching [In reply to] Can't Post


In Reply To

Quote
i use them all the time. maybe you should too.



Its never wise to assume =)


It's wise to learn to spell "It's".

and it is never wise to brag without backing it up. anyone who has 'created' so much perl code who had to ask how to convert my one liner to work in a script, obviously has not done much real perl. so let's see some of this vaunted 'modulated' code. post some here or a url to some. i would love to do a sweet code review of it. just think, you can edjimicate so many others with the kwality of your code!

but you won't do it. you didn't even show a shorter version of my one liner as you claim you could do. you have done nothing here in this thread to warrant even the slightest assumption of your perl virtuosity. so put up or shut up as they say.

i will be waiting to see your code but not holding my breath.

uri


Recall
Novice

Dec 11, 2003, 1:23 PM

Post #16 of 24 (4792 views)
Re: [uri] pattern matching [In reply to] Can't Post


Quote
It's wise to learn to spell "It's".



I only make an effort when I need to Smile


Quote
and it is never wise to brag without backing it up



I certainly wasn't bragging, it was merely something I wanted to point out as you seemed to want to boast about your shorter solution, but as by this point several examples had been given to the user I didn't feel the need to turn it into a golf competition.


Quote
anyone who has 'created' so much perl code who had to ask how to convert my one liner to work in a script, obviously has not done much real perl



Well, actually you've misrepresented what actually happened, I never did ask *how* to use your example in a script, It's more convenient for the person asking the question if you provide cut and paste code, rather that a command line example which is more obscure.


Quote
so let's see some of this vaunted 'modulated' code. post some here or a url to some. i would love to do a sweet code review of it. just think, you can edjimicate so many others with the kwality of your code!



This is where we differ - I don't personally feel the need to show everybody my code or prove how great I am by patronising or belittling people. I am quite content with my level of skill - my scripts do their job and sell well, which is all I'm really concerned with.


Quote
you didn't even show a shorter version of my one liner as you claim you could do



Explained above.


Quote
you have done nothing here in this thread to warrant even the slightest assumption of your perl virtuosity.



Precisely, which is why it refelcts badly on you when you try to put me down without even knowing my ability level =)




Quote
i will be waiting to see your code but not holding my breath.



If you are really that desperate I can show you some, but this users thread is not the place for a penis waving competition.


uri
Thaumaturge

Dec 11, 2003, 1:39 PM

Post #17 of 24 (4789 views)
Re: [Recall] pattern matching [In reply to] Can't Post

well, you certain defended yourself with nothing. you didn't help the OP. you didn't show better code. you didn't explain -l. you didn't show how to convert a one liner to a script. then what help are you at all? writing 'modulated' code is more than just being lazy with it's. in case you don't get it, the word is modular. hard to imagine you write kwality modules when you use the wrong word. how is your dokumentayshun?

all you have done is complain which can be done by any clueless newbie. and selling scripts and writing good perl code have little to do with each other, just look at redmond or matt's archive. and you do have to prove your perl skills here if you are to complain about how others post here. otherwise your opinions will be ignored and lost in the vast net where even google won't find them.

so let's see even one tiny little itsy bitsy sub of yours. you can paste that here or a url. i have nothing to hide. you can find plenty of my code on cpan or on usenet for all to judge. i have rarely found anyone who codes in isolation and thinks their code is great who actually has a good grasp of code or reality. but you will just smirk and put in stupid smiley's and not do anything to remove the stain you have from this thread. oh well, too bad.

uri


davorg
Thaumaturge / Moderator

Dec 11, 2003, 2:29 PM

Post #18 of 24 (4786 views)
Re: [Recall] pattern matching [In reply to] Can't Post

Recall,

The point that you seem to be missing is that the Perl community is a meritocracy. In order to be respected you need to prove your abilities. Uri has proven his abilities many times in various forums, to the best of my knowledge you have not. You should therefore not be surprised if people listen to Uri more than they listen to you.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Dec 11, 2003, 2:55 PM

Post #19 of 24 (4785 views)
Re: [davorg] pattern matching [In reply to] Can't Post

If I may add my comments as a novice Perl coder.

The person that started the thread is most likely even more of a novice than me since they could not figure out even a "long and clumsy" solution like my two solutions.

Seeing a one line command line solution is most likely not going to be a practical solution to a person needing this type of question answered, they will look at it and think "how do I use that in my script?" Its impressive if you look at my answers then look at that nice and compact answer, but if its only offered to make my code look bad then why bother posting it? Anyone can make my code look bad, that is not an accomplishment.

I think what I am saying is you guys up there in the Perl clouds might want to consider the ability of the person asking the question to understand the answer and not post stuff if its just to remind the rest of us hacks how uneducated we are. We already feel dumb enough as it is most of the time asking questions we know others will answer in a second when we may have struggled for a few hours to figure it out.

Anyways, I hate posting this kind of stuff so thats it, flame me or laught at me, ignore me, whatever.
-------------------------------------------------


(This post was edited by KevinR on Dec 11, 2003, 2:57 PM)


Recall
Novice

Dec 11, 2003, 4:07 PM

Post #20 of 24 (4783 views)
Re: [uri] pattern matching [In reply to] Can't Post


Quote
writing 'modulated' code is more than just being lazy with it's. in case you don't get it, the word is modular. hard to imagine you write kwality modules when you use the wrong word. how is your dokumentayshun?



It really seems like you are looking for any possible excuse to make assumptions about my programming abilities. It's quite bizarre. If I use a wrong word then I suppose it *must* mean I'm a useless coder...you win.


Quote
i have rarely found anyone who codes in isolation and thinks their code is great who actually has a good grasp of code or reality. but you will just smirk and put in stupid smiley's and not do anything to remove the stain you have from this thread. oh well, too bad.



More assumptions....I've never labelled my code as great, I've simply felt the need to provide a small amount of defence to your accusations.

I frequently find that the best programmers tend not to patronize or belittle others - they get on with their job, which is where your downfall is...it seems with you its more of an ego trip in which you are intent on proving yourself and your ego can't cope with the idea that there may be others in existance that have perl coding ability also.

I mean, really, what do you want me to show you...something amazing and outstanding?....if so then I can't help....all I can say is that I've written my own collection of perl modules for my needs. I've created a light weight version of CGI.pm without all the bloated html related code, I've created a collection of database modules to interface with MySQL and provide the necessary functionality and flexibility for my scripts...snippet:


Code
  

sub connect {
#----------------------------------------------------------
# Create a database connection.

my $self = shift;
my %conn = %{$self->{connect}};

# No point carring on if we already have a driver.
return 1 if ($self->{driver} and ref($self->{driver}));

# Make sure the connection data exists.
keys(%conn) or return $self->fatal('DB_CONN_KEY');

# Create a new driver object.
my $driv = uc($conn{driver}) || 'MYSQL';
my $pack = qq[PW::DB::Driver::$driv];
my $file = qq[PW/DB/Driver/$driv.pm];

# Require the correct module driver package.
require $file;
my $dobj = $pack->new({
_debug => $self->{_debug},
connect => $self->{connect},
table => scalar($self->name),
schema => $self->{tables}
});
$self->{driver} = $dobj;
$self->{driver}->connect or return;

return 1;
}





My aim in life is not to try and give myself thrills by creating useless (in most cases) one liners, but to making a living by creating useful, flexible and clean code.

I'm sure you'll have comments to make on any code I post as that's the type of person you are - whether I posted something perfect or something utterly rubbish, you'd try to find fault - that comes back to your ego again.

Sigh, I'm losing interest in the discussion and the poor OP must wish we'd all stop ruining his thread.


Recall
Novice

Dec 11, 2003, 4:09 PM

Post #21 of 24 (4782 views)
Re: [davorg] pattern matching [In reply to] Can't Post


Quote
In order to be respected you need to prove your abilities.





Indeed, but not on the demand of wannabe gurus who will pick it apart regardless of it's quality - there's a time and a place.



Quote
You should therefore not be surprised if people listen to Uri more than they listen to you.





I'm not surprised at all, the perl community is oozing with sycophants.


(This post was edited by Recall on Dec 11, 2003, 4:11 PM)


Jasmine
Administrator

Dec 11, 2003, 6:47 PM

Post #22 of 24 (4777 views)
Re: [Recall] pattern matching [In reply to] Can't Post

Recall,

I must interject here. Your first post in this thread was a reply to uri by saying "It can be made even shorter =)". When I first read it, I thought "umm, okay. why didn't he show us the shorter code?", so I think the initial response you received was justified.

I also think that KevinR has a valid point that occasionally the response to a question doesn't always correlate to the experience level of the person asking. While I can see how uri's reponse to john8320 may be difficult for a new user to understand without more explanation, at least he offered assistance.

I don't see how your post contributed anything to the thread, except to inflame someone who was actually trying to help. Additionally, you stated fallacies which when brought to your attention, you reversed position and contradicted yourself. No matter how good a programmer you may be, please remember to keep an open mind.

I found your use of "wannabe gurus" interesting. The dictionary tells us that, among other definitions, that a "guru" is "a recognized leader in a field". By this definition, both uri and davorg are gurus in the perl community, because [url=http://www.sysarch.com/perl/sort_paper.html]their [url=http://www.manning.com/cross/]accomplishments and interest in helping others are well-known.

All I ask of users of this forum is to be civil to each other and try to help those you can. This includes avoiding conflict, refraining from personal attacks, supporting your statements, and once in a while just biting your tongue so that a useless thread like this can disappear into antiquity.

Jasmine


(This post was edited by Jasmine on Dec 11, 2003, 6:49 PM)


davorg
Thaumaturge / Moderator

Dec 12, 2003, 1:52 AM

Post #23 of 24 (4771 views)
Re: [KevinR] pattern matching [In reply to] Can't Post


In Reply To
Seeing a one line command line solution is most likely not going to be a practical solution to a person needing this type of question answered, they will look at it and think "how do I use that in my script?" Its impressive if you look at my answers then look at that nice and compact answer, but if its only offered to make my code look bad then why bother posting it? Anyone can make my code look bad, that is not an accomplishment.


Kevin

I really don't think that Uri posted his solution to make your code look bad. He posted it as a "better" or "more Perlish" solution to the problem.

Personally, I often deliberately don't post a "cut and paste" solution to a problem. After helping out on far too many of these forums for far too long, I've found that all too often if you give someone a cut and paste solution they'll be back with almost exactly the same question a few days later. If you give them an answer that they need to think about then they're more likely to take it in.

Make a man a fire and he'll by warm for a day.
Teach a man how to make a fire and he'll be warm forever.
Throw a man on a fire and he'll be warm for the rest of his life.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Dec 12, 2003, 10:09 AM

Post #24 of 24 (4764 views)
Re: [davorg] pattern matching [In reply to] Can't Post


In Reply To

In Reply To
Seeing a one line command line solution is most likely not going to be a practical solution to a person needing this type of question answered, they will look at it and think "how do I use that in my script?" Its impressive if you look at my answers then look at that nice and compact answer, but if its only offered to make my code look bad then why bother posting it? Anyone can make my code look bad, that is not an accomplishment.


Kevin

I really don't think that Uri posted his solution to make your code look bad. He posted it as a "better" or "more Perlish" solution to the problem.

Personally, I often deliberately don't post a "cut and paste" solution to a problem. After helping out on far too many of these forums for far too long, I've found that all too often if you give someone a cut and paste solution they'll be back with almost exactly the same question a few days later. If you give them an answer that they need to think about then they're more likely to take it in.

Make a man a fire and he'll by warm for a day.
Teach a man how to make a fire and he'll be warm forever.
Throw a man on a fire and he'll be warm for the rest of his life.


Dave,

hehehe.... maybe uri could have used those words (better, more perlish) instead of "long and clumsy". Wink

Anyways, I take no offense from it, I know I am just a novice at this stuff and I appreciate the efforts of people like yourself and uri too that offer suggestions, guidance and the occasional admonition to the unwashed masses.

Your humble narrator.....
Kevin
-------------------------------------------------

 
 


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

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