Gossamer Forum
Home : General : Perl Programming :

Pattern Matchin

(Page 1 of 2)
> >
Quote Reply
Pattern Matchin
Hi

Code:
$val[0] =~ s/^'//;
$val[-1] =~ s/'$//;

How can I consolidate the example into one line ??

Thanks!!

- wil

Last edited by:

Wil: Nov 20, 2001, 8:47 AM
Quote Reply
Re: [Wil] Pattern Matchin In reply to
Have you read the perldoc regex tutorials? Tells you everything you need (almost)

Laugh

As you seem to be using two elements then you can't use one line of code.

Last edited by:

PaulW: Nov 20, 2001, 8:52 AM
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Damn, that's what I was afriad of. It looks like it's begging to be thrown into one line. :-(

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
If it was one element you could do:

$val[0] =~ s,^'|'$,,;

...but not with 2
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
A 'lil off topic, but why do people use commas (,) as the separator thingi? I can't stand it as it's so hard to read.
I'd prefer
s/^'|'$//;
in that case since /'s aren't used
or |'s if |'s aren't used, or #...

Just my $10 CDN (2 cents US [;P])

Adrian
Quote Reply
Re: [brewt] Pattern Matchin In reply to
Just habit I guess

I prefer using , as it lets you see the code within the regex more clearly.

Snippet from my template parser (Im sooooo proud of it):
Code:
$parsed =~ s,<%if\s*(.*?)\s*%>(.*?)<%\s*(.*?)\s*%>(.*?)<%\s*endif\s*%>,if ( $1 =~ m/<\s*(.*?)\s*%>/) { $1 =~ s/$1/$tags{$1} ? $tags{$1} : "Unknown tag: $1"/oesg; } elsif blaaaa ....,oesg;
Just thought I'd be offtopic too :)

Last edited by:

PaulW: Nov 20, 2001, 12:46 PM
Quote Reply
Re: [brewt] Pattern Matchin In reply to
Because I copied an example from a book. That's why <g>.

Will be changing now though - causing way too many headaches.

Just my few worthless euros...

- wil
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Ugh who'd have thought :).....my parser turned out to be crappy pants so I've just spent a few hours re-writing it and now I'm happy :)

Just got to figure out those darn nested if/endif's Unsure
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Hi Paul

Hell of a simple on for you:

Code:
sub parse_template {

local(*FILE); # localise File handle
local($file); # localise File path
local($HTML); # localise HTML data

$file = $_[0];

open (FILE,"<$file") || die("Could not open $file <BR><BR> $!");

while (<FILE>)
{
$HTML .= $_;
}

close(FILE);

$HTML =~ s/<%(\w+)%>/${$1}/g;

print $HTML;
}

That will extract all <%tag%> from your HTML file and replace it with a var inside your sub.

Cheers

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
I'm a bit further on with mine Angelic

It supports normal tags, includes and if/endif's

Attached is a snippet
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Interesting. Mine is fine for the minute. If I wanted something a little more complex, I would go for HTML::Template. Although, it's a great learning exercise!

I don't see where I would want to include files at the moment, or use ifs and buts. I just provide that in the code.

Cheers

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
Yeah but if you have "ifs and buts" in your code it isn't flexible for the user so by putting them into the templates like I've allowed with my parser, it is a much more user friendly option.

I can select from mysql and then in my template add:

<%if Column%>
<%Column%>
<%endif%>

or....

<%if Column%>
<%include something.html%>
<%endif%>

...so it is much more flexible IMO
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Yeah, I get what you're saying. Although I'm only doing this for myself, so I tend to find it OK (obviously!).

Just thinking, though. One drawback with your system is when editing a HTML file in a WYSIWYG editor? Surely it screwes up your layout having 3 difference chuncks of HTML for the ifs, elsif, else ?

If you got the Perl to work that out it would be better because it keps your HTML completly clean.

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
>>Surely it screwes up your layout having 3 difference chuncks of HTML for the ifs, elsif, else ?
<<

Sorry not sure what you mean.
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Well if I'm writing a HTML document, and I have three different tables based upon results of if, elsif and else, the 3 tables would show up in my HTMl document and screw up the layout (while editing local templates).

Putting the if, elsif and else and working that out in the perl program does not do this.

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
Well then don't base your table layout on the tag output Sly

I've been using Links2 for quite a while and never had to do anything like this with the Links2 tags.

It probably boils down to your logic rather than the template parser.
Quote Reply
Re: [Wil] Pattern Matchin In reply to
Hmm spotted a few bugs with my parser and updated it again Blush

0.03s to parse a template (with includes) and to perform 3 SQL queries.

Guess I gotta see how it stands up with rigarous testing :)
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Have you looked at any modules on CPAN which does this? I haven't yet, wondering which is the best? I've heard good things about HTML::Template.

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
No, prefer to write my own modules.

If there is one I don't have time to write or something beyond my knowledge then I'll download it, but if I know I can just about manage it myself then I make it :)
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Yeah, same here. It's funny that though. I take weeks slaving over writing a moduel myself to just about accomplish what I want to do. I learn a hell of a lot in the process too.

Then I sit back, three clicks later at CPAN I have installed a great module that does everything mine wants to and more and even better and much faster. And I think to msyelf..... grrrrrr..

I guess we should welcome this open-source business and just be prepared to accept other's modules. It's like our bodies rejecting other's tissues or something! It's crazy how many people have slaved hours of work into re-creating the wheel. Why?

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
>>It's crazy how many people have slaved hours of work into re-creating the wheel. Why? <<

Because you get more satisfaction knowing it is all your own work than someone elses.
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
Yess, I understand all that. But at the end of the day, you're doing a job. You're trying to achieve something. And if a little trip to CPAN would do everything you needed, then why put yourself through weeks of sleepless nights it?

Always puzzled me this one. I do it,. and thousands of others do it. But the more I read usenet posts these days I notice the tone has changed dramaticaly.

Basicaly, there's a module on CPAN to do just about everything. It would be very rare if you had to write something new because a CPAN module couldn't handle what you're trying to do.

I notice that a large nmumber of posts to usenet these days just gets answered "..go to CPAN. The answers there. The answer is in a module which has been tested, tried, developed and proven to work..." Which is a fair statement. After all, perl hackers have been working sleepless weeks to produce these solid modules, and we go and do it all again.

Kind of like buying a car, then scrapping it all and melting it into metal and building my own. Well, not quite ...

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
>>And if a little trip to CPAN would do everything you needed, then why put yourself through weeks of sleepless nights it?<<

Speak for yourself ...hehe

I can knock up some wicked modules in the time it takes you to figure out what number this prints:

(1+2)+4;

j/k Angelic

But out of interest, without cheating, what do you think it prints?

Then try:

+(1+2)+4;

Last edited by:

PaulW: Nov 21, 2001, 4:52 PM
Quote Reply
Re: [PaulW] Pattern Matchin In reply to
In Reply To:
(1+2)+4;

Well. 1 + 2 = 3. Add 4 is 7.

In Reply To:
+(1+2)+4;

Well. + (1 + 2) = 3. Add 4 is 7.

- wil
Quote Reply
Re: [Wil] Pattern Matchin In reply to
Wrong :)

3 and 7
> >