CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Replace whitespace with hyphen, ASCII code problem instead

 



tomitzel
New User

Oct 11, 2012, 5:05 PM

Post #1 of 8 (1216 views)
Replace whitespace with hyphen, ASCII code problem instead Can't Post

Hello,

I have a string, for eg. I have cars.
I need to replace all whitespaces with hyphens. I use for this the expression:

Code
$fstring =~ s/ /-/g;


But this generates the string:
I,045have,045cars.

Any idea why? If you try to use underscore, it works correctly.

Thanks.


BillKSmith
Veteran

Oct 11, 2012, 7:43 PM

Post #2 of 8 (1212 views)
Re: [tomitzel] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

Works fine for me. (I cannot duplicate the problem.) Please post a complete program which exhibits the symptom.
Good Luck,
Bill


tomitzel
New User

Oct 11, 2012, 11:55 PM

Post #3 of 8 (1210 views)
Re: [BillKSmith] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

I am just going to post the subroutine in which it is that line, the file has over 2000 lines of code.

Code
sub encode_dir 
{

my ($fstring) = @_;
my ($ms, $enc_string, $oneletter, $bstr);

$fstring =~ s/ /-/g;

for ($ms = 0; $ms < length($fstring); $ms++)
{

$oneletter = substr($fstring, $ms, 1);

if (($oneletter !~ /[0-9a-zA-Z]/) and ($oneletter ne "-"))
{
$bstr = ord($oneletter);
if (length($bstr) == 1) {$bstr = "00" . $bstr;}
if (length($bstr) == 2) {$bstr = "0" . $bstr;}
$bstr = "," . $bstr;
$enc_string = $enc_string . $bstr;
}
else
{
$enc_string = $enc_string . $oneletter;
}
}

return ($enc_string);

}


Code



      
    


Laurent_R
Veteran / Moderator

Oct 11, 2012, 11:56 PM

Post #4 of 8 (1209 views)
Re: [tomitzel] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

I also absolutely don't see how this regular expression would have such effect. Please provide a complete example showing your problem.


tomitzel
New User

Oct 12, 2012, 12:10 AM

Post #5 of 8 (1207 views)
Re: [Laurent_R] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

I've ran myself also that subroutine and in indeed the problem doesn't seem to be there.

After that encoding I am making a directory out of the string, could it be that function mkdir causes that?


FishMonger
Veteran / Moderator

Oct 12, 2012, 7:44 AM

Post #6 of 8 (1203 views)
Re: [tomitzel] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

I'm able to duplicate your problem if I remove and ($oneletter ne "-") from the conditional, which must be what you did when you experienced the problem.


FishMonger
Veteran / Moderator

Oct 12, 2012, 8:09 AM

Post #7 of 8 (1198 views)
Re: [tomitzel] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

BTW, your sub could be reduced and made more efficient and easier to read/understand/maintain.


Code
use 5.10.0; 
use strict;
use warnings;

my $str = 'I have cars';
$str = encode_dir($str);
say $str;


sub encode_dir {
my @chars = split('', $_[0]);
foreach (@chars) {
next if tr / /-/;
next if /[[:alnum:]]/;
$_ = ',' . sprintf("%03d", ord $_);
}
return join('', @chars);
}



BillKSmith
Veteran

Oct 12, 2012, 9:04 AM

Post #8 of 8 (1192 views)
Re: [FishMonger] Replace whitespace with hyphen, ASCII code problem instead [In reply to] Can't Post

Even easier (and works with much older versions of perl).

Code
use strict; 
use warnings;
my $str = 'I have cars.';
$str = encode_dir($str);
print $str;
sub encode_dir {
my ($str) = @_;
$str =~ tr / /-/;
$str =~ s/([^-\da-zA-Z])/sprintf '%03d', ord $1/ge;
return $str;
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives