Sunday, August 2, 2009

How to convert character space into a hyphen in perl?

I have a perl script that generates static html pages based on company name. However, it is written in such a way that the html pages are created with spaces in them ( because company name will have space).





Is there anyway I can convert spaces into Hyphens?.

How to convert character space into a hyphen in perl?
Kolmogorov is right, but here's how it would apply to your code:





my $title_no_spaces = $rec{'Title'} =~ s/ /-/g;





my $_file = $title_no_spaces."-".$id.$build_e...
Reply:this is a script that will convert all the spaces in the file called a.txt into hyphens and store the result in the file a2.txt





if by $_file you mean it is the file name then replace a.txt with $_file





open (FILE, "a.txt");


open OUTPUT, "%26gt;a2.txt"


my @lines = %26lt;FILE%26gt;;


foreach(@lines){


s/\ /-/g;


print OUTPUT "$_ \n";


}
Reply:I don't know Perl per se, but the generalised method would be to parse the string char by char, replacing all occurences of " " with "-". It shouldn't be any more complicated than a short for-loop.


No comments:

Post a Comment