Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Stupid perl question.

Quote Reply
Stupid perl question.
Hi:



I have a global to return a random category name, and I want to use it for linking to the category. I have a working global like this:

Quote:


sub {
no strict;
my $count = int(rand($DB->table('Category')->count)) || 1;
my $link = $DB->table('Category')->select( { ID => $count }, ['Full_Name'] )->fetchrow;
$link =~ s/\s/_/g;
$link =~ s/\./_/g;
$link =~ s/&/_/g;
$link =~ s/\?/_/g;
$link =~ s/!/_/g;
$link =~ s/\*/_/g;
$link =~ s/'/_/g;
$link =~ s/\(/_/g;
$link =~ s/\)/_/g;
$link =~ s/:/_/g;
$link =~ s/,/_/g;
return $link;
}


I guess you can see why I want to clean it up.... all those subsitutions- while neccisary- are ugly. I was wondering if this is the same thing:



Quote:


and will that work?



Thanks!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Stupid perl question. In reply to
Code:
sub {
my ($cat, $link);

$cat = $DB->table('Category');
$cat->select_options('ORDER BY RAND()', 'LIMIT 1');
$link = $cat->select( 'Full_Name' )->fetchrow;
$link =~ s/\W/_/g;
return $link;
}

Something like that may work.

Last edited by:

Paul: Aug 15, 2002, 1:58 PM
Quote Reply
Re: [carfac] Stupid perl question. In reply to
This piece of code (on the basis of what you have) should work:
Code:
sub {
my $ct = $DB->table('Category');
my $count = int(rand($ct->count)) || 1;
my $full_name = $ct->select( { ID => $count }, ['Full_Name'])->fetchrow;
return $ct->as_url($full_name);
}
BTW: why do you turn off strict (and then still use my's to declare your variables)?

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Stupid perl question. In reply to
Hi:



why do you turn off strict (and then still use my's to declare your variables)?



I dunno. It seems like it is there is a lot of globals, and I do not know what it does, so I just do not mess with it.



As to using "/W", that changes "-" into whatever (in this case, "_").... but, for a real link to work, the "-" must remain "-", so that will not work, and that is why I was busy trying to find all the non-alphanumeric characters I use EXCEPT "-" and change them!!!!

It's no biggy, it works now.... I just wanted to make it better!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Stupid perl question. In reply to
Oh, it looks like the new version of the code dissapeared.... what I meant ot write was:



$link =~ s/\(\s|\.|&|\?|!|\*'|\(|\)|:|,\)/_/g;
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Stupid perl question. In reply to
Have you tried my solution (which is much better, I think).Wink

use/no strict:
http://www.perldoc.com/....6.1/lib/strict.html

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [carfac] Stupid perl question. In reply to
if you insist on using a regex, use

$link =~ s/[^\w\d_\-\/]/_/g;

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Stupid perl question. In reply to
Yogi:



Sorry- I had not tried that yet.... but I have nbow! Looks like it works perfect! Thank you!
dave

Big Cartoon DataBase
Big Comic Book DataBase