Jump to content

Camel case and snake case: Difference between revisions

Line 24:
 
 
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Camel_case_and_snake_case
use warnings;
 
my @words = (
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "#rgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "
);
 
sub tosnake
{
shift =~ s/^ +| +$//gr =~ s/[A-Z]/_\l$&/gr =~ tr/ -/_/r =~ s/__+/_/gr;
}
 
sub tocamel
{
shift =~ s/^ +| +$//gr =~ s/[ _-]([a-z0-9])/\u$1/gir;
}
 
print "to snake case\n\n";
for my $word ( @words )
{
printf "%35s -> %s\n", $word, tosnake($word);
}
 
print "\nto camel case\n\n";
for my $word ( @words )
{
printf "%35s -> %s\n", $word, tocamel($word);
}</lang>
{{out}}
<pre>
to snake case
 
snakeCase -> snake_case
snake_case -> snake_case
variable_10_case -> variable_10_case
variable10Case -> variable10_case
#rgo rE tHis -> #rgo_r_e_t_his
hurry-up-joe! -> hurry_up_joe!
c://my-docs/happy_Flag-Day/12.doc -> c://my_docs/happy_flag_day/12.doc
spaces -> spaces
 
to camel case
 
snakeCase -> snakeCase
snake_case -> snakeCase
variable_10_case -> variable10Case
variable10Case -> variable10Case
#rgo rE tHis -> #rgoRETHis
hurry-up-joe! -> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
spaces -> spaces
</pre>
 
=={{header|Phix}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.