Gossamer Forum
Home : Products : DBMan : Customization :

Help with simple perl code

Quote Reply
Help with simple perl code
I need help from someone familiar with perl. In my "sub html_record" I want to do a simple display script that says,

If sex=male print "Actor"
If sex=female print "Actress"

The "sex" is of course one of my dbman field definitions.

Thanks
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
Code:
if ($rec{sex} =~ /^male$/i) {
print "Actor"
}
elsif ($rec{sex} =~ /^female$/i) {
print "Actress"
}

or you could do....

my %match = ( male => 'Actor', female => 'Actress' );

print $match{$rec{sex}};

Last edited by:

RedRum: Jan 22, 2002, 2:13 PM
Quote Reply
Re: [RedRum] Help with simple perl code In reply to
Let me ask you to take this one step further.

Now I want to do this:

If category="Actor/Actress" then [find out if they are male or female and print Actor or Actress]

else print $rec{category}

Can I do that?
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
>>[find out if they are male or female and print Actor or Actress] <<

Im not sure what you mean. If you are printing actor/actress why do you need to find out if they are male/female?

printing $rec{category} would do what you seem to require.

If you want to print their sex you'd use something like:

my %match = ( Actor => 'Male', Actress => 'Female' );

print $rec{category} =~ /(Actor|Actress)/i ? $match{$1} : $rec{category};

Last edited by:

RedRum: Jan 22, 2002, 2:45 PM
Quote Reply
Re: [RedRum] Help with simple perl code In reply to
Sorry for confusion. I meant that if the category is "Actor/Actress" to print "Actor" if they are male or "Actress" if they are female. Otherwise, just print the category name.

Also, The actual name of the category is "Actor/Actress" so I have to be mindful of the "/" character in the code. Also, this is going to be inserted into a print qq| ... |; so your previous post with a "|" character isnt working.

Thanks for your help

Last edited by:

cap1tan: Jan 22, 2002, 2:54 PM
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
After the previous post, I sorted through the code you wrote and what I have and it seems to be half working - this is what i have:

my %match = ( Male => 'Actor', Female => 'Actress' );

print qq|blah blah..|; print $rec{Category} =~ /(Male|Female)/i ? $match{$1} : $rec{Category};

print qq| blah blah...

What I mean by half working is the script runs with no errors, but the result is just the Category name. It is not printing "Actor" for male or "Actress" for female. It just prints the full name "Actor/Actress".
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
Why not simplify things and just make the category field be a select field with the options: Actor, Actress

or make it a radio select field?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
If it is printing the category, that means that it isn't matching Male or Female.

What is printed?

Last edited by:

RedRum: Jan 22, 2002, 5:11 PM
Quote Reply
Re: [RedRum] Help with simple perl code In reply to
What i mean is: it just prints the category name, just as if all I had there was $rec{'Category'}. So record in the Director category print "Director" and the Actor/Actress category just prints "Actor/Actress."

I know this sounds like i could do it simpler by just making two different categories, but I already have a field for "sex" and i dont want redundant information. The basic formula i want to make work is:

if "Catagory" = "Actor/Actress" then
if sex = male print "Actor"
if sex = female print "Actress"
else print $rec{'Category'}

make sense now?
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
Try:

Code:
my %match = ( male => 'Actor', female => 'Actress' );

if ($rec{Category} =~ m,^Actor/Actress$,i) {
print $match{$rec{sex}};
}
else {
print $rec{Category};
}

That should do as you require as long as all the field names are correct etc.

Last edited by:

RedRum: Jan 23, 2002, 8:47 AM
Quote Reply
Re: [RedRum] Help with simple perl code In reply to
Thank you so much! It worked!!

I had to change "if {" to "if (" but that's alright. I know you experts are allowed to slip up every now and then. Wink

Thanks again!
Quote Reply
Re: [cap1tan] Help with simple perl code In reply to
Oops that was a typo. I spotted it whilst I was writing the code and forgot to edit it. I also spelt Category wrong :)

Last edited by:

RedRum: Jan 23, 2002, 8:47 AM