Gossamer Forum
Home : General : Perl Programming :

regex problem...

Quote Reply
regex problem...
Hi

I am a kind of newsbie in perl and in regex

I would like to applys many regex to (my $cat_name_e = $cat->{cat_name})

So here is what I did : regex are good (I guess) but it is to way I did apply these regex to (my $cat_name_e = $cat->{cat_name}) that is wrong...

(my $cat_name_e = $cat->{cat_name}) =~s/[éèê]/e/g; #replace accent
(my $cat_name_e = $cat->{cat_name}) =~s/[àâ]/a/g; #replace accent
(my $cat_name_e = $cat->{cat_name}) =~s/ô/o/g; #replace accent
(my $cat_name_e = $cat->{cat_name}) =~s/î/i/g; #replace accent
(my $cat_name_e = $cat->{cat_name}) =~s/ç/c/g; #replace accent
(my $cat_name_e = $cat->{cat_name}) =~s/\W/-/g; #replace all non alphanumerics by hyphens
(my $cat_name_e = $cat->{cat_name}) =~s/---/--/g; #replace of hyphens
(my $cat_name_e = $cat->{cat_name}) =~s/--/-/g; #replace of hyphens


Txs
FMP




Quote Reply
Re: [fmp] regex problem... In reply to
Hi FMP

try to replace $cat->{cat_name} with $cat{cat_name}.

Cheers,
Oliver
Quote Reply
Re: [olivers] regex problem... In reply to
Hi

Txs for you answer

I have done the replace but I have a compilation error message

Regards
FMP
Quote Reply
Re: [fmp] regex problem... In reply to
I guess the error looks like this:
"my" variable $cat_name_e masks earlier declaration in same scope at scriptfile.pl line X.

Only use "my" the first time before $cat_name_e and leave it away afterwards. Your code would look like this:

Code:

(my $cat_name_e = $cat->{cat_name}) =~s/[éèê]/e/g; #replace accent
($cat_name_e = $cat{cat_name}) =~s/[àâ]/a/g; #replace accent
($cat_name_e = $cat{cat_name}) =~s/ô/o/g; #replace accent
($cat_name_e = $cat{cat_name}) =~s/î/i/g; #replace accent
($cat_name_e = $cat{cat_name}) =~s/ç/c/g; #replace accent
($cat_name_e = $cat{cat_name}) =~s/\W/-/g; #replace all non alphanumerics by hyphens
($cat_name_e = $cat{cat_name}) =~s/---/--/g; #replace of hyphens
($cat_name_e = $cat{cat_name}) =~s/--/-/g; #replace of hyphens



Hope this helps
Oliver
Quote Reply
Re: [olivers] regex problem... In reply to
Why not use something like this? Or am I missing the point?

Code:
my $cat_name_e = $cat->{cat_name};

$cat_name_e =~s/[éèê]/e/g; #replace accent
$cat_name_e =~s/[àâ]/a/g; #replace accent
$cat_name_e =~s/ô/o/g; #replace accent
$cat_name_e =~s/î/i/g; #replace accent
$cat_name_e =~s/ç/c/g; #replace accent
$cat_name_e =~s/\W/-/g; #replace all non alphanumerics by hyphens
$cat_name_e =~s/---/--/g; #replace of hyphens
$cat_name_e =~s/--/-/g; #replace of hyphens

... or even better (probably);

Code:
my $cat_name_e = $cat->{cat_name};

$cat_name_e =~s/[éèê]/e/g; #replace accent
$cat_name_e =~s/[àâ]/a/g; #replace accent
$cat_name_e =~s/\Qô/o/g; #replace accent
$cat_name_e =~s/\Qî/i/g; #replace accent
$cat_name_e =~s/\Qç/c/g; #replace accent
$cat_name_e =~s/\W/-/g; #replace all non alphanumerics by hyphens
$cat_name_e =~s/\Q---/--/g; #replace of hyphens
$cat_name_e =~s/\Q--/-/g; #replace of hyphens

(i.e with the \Q to escape the non-standard charchters, which could be causing the problem).

Hope that helps.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Jul 8, 2004, 4:25 AM
Quote Reply
Re: [Andy] regex problem... In reply to
Hi Andy

thanks for your inputs - you're absolutely right that it's not necessary to repeat $cat_name_e = $cat{cat_name} on every line. And your code version is smaller Cool.

Some additional comments/questions:

- my $cat_name_e = $cat->{cat_name} didn't work for me...
I had to use $cat_name_e = $cat{cat_name}.

- didn't you mean $cat_name_e =~... on line 3+ ?


All the best,
Oliver
Quote Reply
Re: [olivers] regex problem... In reply to
Hi,

Quote:
- didn't you mean $cat_name_e =~... on line 3+ ?

Whoops Blush I've fixed this up in the above post.

Quote:
- my $cat_name_e = $cat->{cat_name} didn't work for me...
I had to use $cat_name_e = $cat{cat_name}.

It depends on where $cat is being defined.

It could either be;

$cat->{var_name}

...or

$cat{var_name}

FMP: Can we see where the $cat variable is generated?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] regex problem... In reply to
Thanks guys it works Wink

I use these REGEX in the globals.txt file of the search_engine template of gossamer Forum product

I just want to make some changes in the generated URL

Original file was

# This file is auto generated and contains a perl hash of
# your template globals for 'default' template set.
# Generated on: Sat Jan 12 12:43:20 2002
# vim: syn=perl
{
'cat_full_name_linked' => q²sub {
my ($id, $sep, $pre, $suf) = @_;
my $cat_path = GT::Template->tags()->{cat_path}; # See "cat_path" global var below
$sep ||= ': ';
$pre ||= '';
$pre =~ s/%/%%/g;
$suf ||= '';
$suf =~ s/%/%%/g;
my $url = '/forum';
my $category = qq|<a href="%s">$pre%s$suf</a>|;
if (!$GForum::CAT_CACHE{$id}) {
my $Cat = $DB->table('Category');
my $cat = $Cat->get({ cat_id => $id }, 'HASH', ['cat_id', 'cat_name', 'cat_id_fk']);
push @{$GForum::CAT_CACHE{$id}}, $cat;
while ($cat = $Cat->get({ cat_id => $cat->{cat_id_fk} }, 'HASH', ['cat_id', 'cat_name', 'cat_id_fk'])) {
push @{$GForum::CAT_CACHE{$id}}, $cat;
}
}
my @cats = @{$GForum::CAT_CACHE{$id}};
my $cat = shift @cats;
my $cat_name = sprintf($category, ${$cat_path->($id)}, $cat->{cat_name});
for (@cats) {
$cat_name = sprintf($category, ${$cat_path->($_->{cat_id})}, $_->{cat_name}) . $sep . $cat_name;
}
return \$cat_name;
}²,
escape_name => q²sub {
my $name = shift;
$name =~ y/ \t\r\n?"'#/__/d;
$name = $IN->escape($name);
$name =~ s|%2F|/|gi; # For some reason, Apache doesn't like %2F in the pathinfo, but a real / is fine
\$name;
}²,
cat_path => q²sub {
my $id = shift;
my $category = q|%s_C%d/|;
if (!$GForum::CAT_CACHE{$id}) {
my $Cat = $DB->table('Category');
my $cat = $Cat->get({ cat_id => $id }, 'HASH', ['cat_id', 'cat_name', 'cat_id_fk']);
push @{$GForum::CAT_CACHE{$id}}, $cat;
while ($cat = $Cat->get({ cat_id => $cat->{cat_id_fk} }, 'HASH', ['cat_id', 'cat_name', 'cat_id_fk'])) {
push @{$GForum::CAT_CACHE{$id}}, $cat;
}
}
my @cats = @{$GForum::CAT_CACHE{$id}};
my $cat = shift @cats;
(my $cat_name_e = $cat->{cat_name}) =~ y/ \t\r\n?"'#/__/d;
my $cat_name = sprintf($category, $cat_name_e, $id);
for (@cats) {
($cat_name_e = $_->{cat_name}) =~ y/ \t\r\n?"'#/__/d;
$cat_name = sprintf($category, $cat_name_e, $_->{cat_id}) . $cat_name;
}
return \"/forum/$cat_name";
}²,
};
Quote Reply
Re: [Andy] regex problem... In reply to
Would this not be a good time to use tr/ instead of multiple s/ (to replace the accents, not the hyphen stuff)?

I use this in one of my scripts:
Code:
$accentless =~ tr/áàâäãéèêëíìîïóòôöõúùûüýÿçñ/aaaaaeeeeiiiiooooouuuuyycn/;
$accentless =~ tr/ÁÀÂÄÃÉÈÊËÍÌÎÏÓÒÔÖÕÚÙÛÜÝŸÇÑ/aaaaaeeeeiiiiooooouuuuyycn/;

Might be overkill in this case but it gets the job done for me.

Thoughts?