Gossamer Forum
Home : Products : DBMan : Customization :

Understanding Proper casing.

Quote Reply
Understanding Proper casing.
I'm trying to understand proper case in perl. What I would like to happen is when someone enters something in a field no matter how they enter it (all CAPS or all lower) it will convert it to Proper Case.


Here is what I tried so far:


In the sub html_record_form {


<input name="Company" value="$rec{'Company'}=~s/([w']+)//g" size="40" type="text">


What showed up was this in the input box: =~s/([w']+)//g




In the sub html_record {


$rec{'company'}=~s/([w']+)//g


Here the company name showed up with this on the end of the record: =~s/([w']+)//g


From what I read this statement should convert this to proper caes. I guess I don't understand how to use it correctly.
Quote Reply
Re: [knue] Understanding Proper casing. In reply to
Hi,

Sorry - I'm not sure what you are after. You want to lowercase stuff?

You can do it pretty simply without a regex:

Code:
lc(ucfirst($word))

So in your example, it would be something like:

Code:
$rec{Company} = lc(ucfirst($rec{Company}))

That will first of all lowercase the whole string, and then uppercase just the first word

Quote:
What showed up was this in the input box: =~s/([w']+)//g

You are trying to do a regex replacement, direct in the string :) You need to do it BEFORE the string, otherwise it will be printed out literally.

Quote:
$rec{'company'}=~s/([w']+)//g[

You have the case wrong - "company" should be "Company"

This is why using all lowercase stuff is easier, as it means you don't make mistakes like that :)

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] Understanding Proper casing. In reply to
Hello Andy,

What I'm trying to do is change the entries to proper case, exp.

If they enter: "state farm ins." I want that to save as "State Farm Ins."

Or

If they enter: "STATE FARM INS." I want that to save as "State Farm Ins.".

I used your code and got the same results.

$rec{Company} = lc(ucfirst($rec{Company}))

STATE FARM INS. = lc(ucfirst(STATE FARM INS.))

Any ideals?

thanks
Ed-
Quote Reply
Re: [knue] Understanding Proper casing. In reply to
Hi,

What is the exact code you are using? Maybe put the whole function in (or at least the part where you save, and print out the string). It sounds like you are still trying to do it inside the string thats being printed, thus its not actually being read (but printed out instead)

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] Understanding Proper casing. In reply to
you shouldn't do it in the form (html_record_form). you need to do it before you add or modify the record. i think you already have a sub for before_add_record and before_modify_record. so there is where you put the regex to convert the value to proper case. or, as andy mentioned, you could just store the record however it's entered (all caps, all lower, or whater); then do the regex before you display it in html_record.

not sure, but i found this online:
/\b[\w']+\b/g # andy help?


so if you store as entered, and convert when displayed, you would have:

my ($company) = $rec{'Company'};

$company =~ /\b[\w']+\b/g;

then instead of printing $rec{'Company'} you would print $company.

Last edited by:

delicia: Jan 13, 2016, 7:19 AM
Quote Reply
Re: [delicia] Understanding Proper casing. In reply to
Hi,

If he is wanting to correct the info given by someone, agreed it should be done when saving. You really want a little function, where you pass in a string, and it spits out a nicely formatted one:

Code:
sub convert_case {
my @back;
foreach (@split / /, $_[0]) {
push @back, lc(ucfirst($_));
}
return join " ", @back;
}

Call with:

Code:
$rec{'Company'} = convert_case($rec{'Company'})

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] Understanding Proper casing. In reply to
Hey guys,

So far no luck on making this work.

What I'm trying to do is when someone adds a record or modify one it will save they're entry as a proper case.
Example: "STATE FARM" will save as "State Farm".

I tried using this:

sub convert_case {
my @back;
foreach (@split / /, $_[0]) {
push @back, lc(ucfirst($_));
}
return join " ", @back;
}

But I keep getting and error.

I put it in my dg.cgi and I tried it in my html.pl (not at the same time) and I used this to call it:
$rec{'Company'} = convert_case($rec{'Company'}). I put this at the top of my "after_add_record

sub after_add_record {
#---------------------------------------------
# update db2

# first get the new record
%rec = &get_record($in{$db_key});
$rec{'Company'} = convert_case($rec{'Company'})
my (%rec2) = %rec;
my ($db2_file_name) = "$db_after_add_db";


Any ideals?
Ed-
Quote Reply
Re: [knue] Understanding Proper casing. In reply to
I do wish you would use the [code][/code] tags to format your code. Makes it so hard to read :(

Quote:
But I keep getting and error.

Whats the error?

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: [knue] Understanding Proper casing. In reply to
you need to convert before you add the record, not after, don't you?
(unless you only want to update in db2, not db1)

Last edited by:

delicia: Jan 18, 2016, 5:33 AM
Quote Reply
Re: [delicia] Understanding Proper casing. In reply to
delicia,

Correct, I need to convert it before it is save in both db1 and db2.

Sorry andy, I'll start using the /code/ tags in my post.

I removed all this code from my script to start over, i had bits and pieces all over the place, figure it would be best to have a clean
script (my script) to work with.

Ed-