Gossamer Forum
Home : General : Perl Programming :

Converting a simple Javascript to Perl

Quote Reply
Converting a simple Javascript to Perl
Hi,

I think i posted my help on a different place but here goes again. I would like the following javascript code converted to perl code. I like the way this javascript works and i want that same thing for perl. So if anybody can help me with that i would appreciate it. Here is the code:

function cleanCAPS(str) {
capsallowed = 3; // Lowercase if more than ## CAPS in a row
do {
eval("re = /([A-Z] {" + (capsallowed+1) + ",})/g;");
myArray = str.match(re);
if (myArray) {
eval("re = /" + myArray[0] + "/;");
str = str.replace(re, ""+myArray[0].toLowerCase());
}
} while (myArray);
return str;
}


Thanks in advance!
Quote Reply
Re: [llccoo] Converting a simple Javascript to Perl In reply to
A basic one would be;

Code:
print &capitalize("this IS Just a test");

###############################################################
# capitalize all the words in the given string. very useful.
sub capitalize {

my $string = lc($_[0]);

my @cut = split / /,$string;

my $new;
my $count = 0;
foreach my $word (@cut) {
$new = $new . ucfirst $word . " ";
}

chop $new; #get rid of trailing blank

return $new;


}

The above would produce;

This Is Just A Test

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: [llccoo] Converting a simple Javascript to Perl In reply to
Well the script is supposed to take in a string that is ALL CAPS and keep the words that are less or equal to the constant capsallowed which in this case is 3. So for a constant of 4 and i send in a string of:
AA iS UP 10% IN NYC --> i would get AA is up 10% in NYC

NASA DOWN 2% IN WORKFORCE --> i would get NASA down 2% in workforce

etc ...

Hope this clears things up ... If you have anymore clarifying let me know. Thanks for you help.
Quote Reply
Re: [llccoo] Converting a simple Javascript to Perl In reply to
Not a very effecient way to do it, but how about this?

Code:
my $string = 'NASA DOWN 2% IN WORKFORCE';

$string = lc($string);

my @sliced = split / /, $string;

my $back;
my $count = 0;
foreach my $line (@sliced) {
if ($count == 0) { $back = uc($line); }
if ($count > 0) { $back .= " " . $line; }
$count++;
}

print $back;

This *should* give you;

NASA down 2% in workforce

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!

Last edited by:

Andy: Dec 22, 2003, 6:41 AM
Quote Reply
Re: [Andy] Converting a simple Javascript to Perl In reply to
thank so much for the reply. I will give this a test today. Just a small question though, how would i control the constant or how many CAPS is allowed before it makes them lowercase.
Quote Reply
Re: [llccoo] Converting a simple Javascript to Perl In reply to
No prob. That routine simply capitalizes the first word, and then the rest all all put to lowercase (with lc()).

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] Converting a simple Javascript to Perl In reply to
Well it works for the first word and for some weird reason it puts all the string in one word. Anyway of making it so that not just the first word is kept CAPped? The javascript keeps the word constant as long as it is less then or equal to the constant "capsallowed". if the word is longer then that make it lowercase...
Quote Reply
Re: [llccoo] Converting a simple Javascript to Perl In reply to
I came up with something like the following but still not like the javascript above:

sub capitalize {
my $string = lc($_[0]);
my @cut = split / /,$string;
my $new = " ";
my $word = " ";

foreach $word (@cut) {
if (length($word) <= 3) {
$new = $new . uc $word . " ";
} else {
$new = $new . $word . " ";
}
}
chop $new; #get rid of trailing blank

return $new; #return the string
}

This is the closest i have gotten to the javascript.