Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Perl string comparison question

Quote Reply
Perl string comparison question
Hi,

Im working on a HTML drop down menu using a Global that loops through all the Categories and adds them into a Select menu with a <option selected> if this page matches the current category.

for example, the current category is: Foo / Blue

Category 1: <option value=/foo/">Foo</option>
Category 2: <option value=/foo/blue/" selected>Foo / Blue</option>
Category 3: <optionvalue=/foo/red/">Foo / Red</option>

The trouble im having is when i try and setup a string comparison Perl deosn't seem to be matching the strings correctly.

For example if $ThisDirectoryName = foo and $DirName = foo the following conditional work fine.

Code:
if ($ThisDirectoryName == $DirName) {
$output .= qq~\n<option value="/splash/$DirectoryName/" selected> $heading</option>~;
} else {
$output .= qq~\n<option value="/splash/$DirectoryName/"> $heading</option>~;
}


but when $ThisDirectoryName = foo/blue and $DirName = foo/blue the conditional returns true for all categories and adds a selected to each option.

Here's the full Global im using...

Code:
sub {
# genre_cetegories
my $Type = shift;
my $ThisDirectoryName = shift;
my $cat_db = $DB->table('Category') || return $GT::SQL::error;
$cat_db->select_options('ORDER BY Full_Name') || return $GT::SQL::error;
my $sth = $cat_db->select ({Type => $Type}, ['ID','DirectoryName','Full_Name']) || return $GT::SQL::error;

my $output=qq~<form style="margin:0;margin-left:10px;"><select name="genremenu" onchange="selectgenremenu(this.form);">~;
my $i=0;
while (my ($ID,$DirectoryName,$heading) = $sth->fetchrow_array) {
$i++;

my $DirName = $DirectoryName;
$DirName =~ s,/,,g;
$ThisDirectoryName =~ s,/,,g;

if ($ThisDirectoryName == $DirName) {
$output .= qq~\n<option value="/splash/$DirectoryName/" selected> $heading</option>~;
} else {
$output .= qq~\n<option value="/splash/$DirectoryName/"> $heading</option>~;
}
}
$output .= qq~</select></form>~;
return $output;
}

and how its called from Category.html <%genre_cetegories($Type,$DirectoryName)%>

Was thinking the forward slashes in the string ie. foo/blue may have been causing the error so i added a search and replace to remove them...

my $DirName = $DirectoryName;
$DirName =~ s,/,,g;
$ThisDirectoryName =~ s,/,,g;

With no luck.

Are there any error's you can see in this global?

thanks,



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Perl string comparison question In reply to
Hi,

I don't think that == will work, as thats for numerical comparisons.

i.e;

if ($i == 50) { .... }

You could try;

$ThisDirectoryName eq $DirName

..or if that fails, try a regex comparison;

$ThisDirectoryName =~ /\Q$DirName/

Hope that helps :)

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] Perl string comparison question In reply to
 
Cheers Andy! I didn't use this method in the end.



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile