how do I replace all the spaces in a string with +
I have to changes all the spaces in a string to +'s. Is there an easy =
way to do this. The length of the string and the number of spaces will =
always be changing. =
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Greetings,
On Fri, Feb 19, 2010 at 03:54:27PM -0500, Erik Lewis wrote:
> I have to changes all the spaces in a string to +'s. Is there an easy way to do this. The length of the string and the number of spaces will always be changing.
$string =~ s/ /\+/g;
That is all. All spaces will be replaced.
--
Happy hacking, Sergey Matveev
FSF Associate member #5968 | FSFE Fellow #1390
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Thanks Sergey,
That was just staring at me in the face.
If anyone is interested a little code to spit back the latitude and =
longitude of an address. Next step is to get it too pull the address =
from a mysql database and then update it.
++++++++++++++++++++++++++++++++
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
print "Enter your address\n";
chomp (my $rawaddress =3D <>);
my $geoaddress =3D $rawaddress =3D~ s/ /\+/;
#strip the spaces from the address
my $googlekey =3D =
"ABQIAAAAJKeZa28YtErALcrbEC0UlBREf5oWR6F07BQvSEe3pww8R4s0VhT fTt-19vTI9qA-_=
V1pUf4-_TcfpQ";
#get your google http://code.google.com/apis/maps/signup.html
my $geocode_csv =3D =
get("http://maps.google.com/maps/geo?q=3D$geoaddress&output= 3Dcsv&sensor=3D=
false&key=3D$googlekey")
or die 'Unable to get page';
#return the google csv data
my [at] geoarray =3D split(/,/, $geocode_csv);
#break the csv into fields
print "$geoarray[2],$geoarray[3]\n";
#print the longitude and latitude
exit 0;
++++++++++++++++++++++++++++++++
On Feb 19, 2010, at 4:00 PM, Sergey Matveev wrote:
> Greetings,
>
> On Fri, Feb 19, 2010 at 03:54:27PM -0500, Erik Lewis wrote:
>
>> I have to changes all the spaces in a string to +'s. Is there an =
easy way to do this. The length of the string and the number of spaces =
will always be changing.
>
> $string =3D~ s/ /\+/g;
>
> That is all. All spaces will be replaced.
>
> --
> Happy hacking, Sergey Matveev
> FSF Associate member #5968 | FSFE Fellow #1390
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Sergey Matveev wrote:
> Greetings,
>
> On Fri, Feb 19, 2010 at 03:54:27PM -0500, Erik Lewis wrote:
>
>> I have to changes all the spaces in a string to +'s. Is there an
>> easy way to do this. The length of the string and the number of
>> spaces will always be changing.
>
> $string =~ s/ /\+/g;
>
> That is all. All spaces will be replaced.
You don't have to escape a plus sign in a quoted string:
$string =~ s/ /+/g;
Or for a more efficient way:
$string =~ tr/ /+/;
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
>>>>> "SM" == Sergey Matveev <stargravesm [at] gmail.com> writes:
SM> $string =~ s/ /\+/g;
why are you escaping the + there? that is a replacement string, not a
regex. it is double quotish but not much more than that. + is just a
regular char there like almost all chars in double quoted strings.
this is a common thing i have seen with newbies. the left side of s///
is a regex and you may need escape metachars. the right side is just a
double quoted string.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Greetings,
On Fri, Feb 19, 2010 at 02:23:07PM -0800, John W. Krahn wrote:
> You don't have to escape a plus sign in a quoted string:
>
> $string =~ s/ /+/g;
Yeah. I see. I understand my fault.
--
Happy hacking, Sergey Matveev
FSF Associate member #5968 | FSFE Fellow #1390
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Erik Lewis wrote:
> I have to changes all the spaces in a string to +'s. Is there an easy way to do this. The length of the string and the number of spaces will always be changing.
You are probably looking for tr, see perldoc -f tr.
Do you mean chr(32) only, or also other types of space?
To for example replace space and tab runs by a single plus, try this:
s{ [[:blank:]]+ } {+}xg;
which is equivalent to
tr { \t} {+}s;
Example:
perl -wle '
my $var = "abc def \t g";
(my $v1 = $var ) =~ s{ [[:blank:]]+ } {+}xg;
(my $v2 = $var ) =~ tr { \t} {+}s;
print "<$_>" for $var, $v1, $v2;
'
<abc def g>
<abc+def+g>
<abc+def+g>
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
On Fri, Feb 19, 2010 at 3:23 PM, John W. Krahn <jwkrahn [at] shaw.ca> wrote:
> Sergey Matveev wrote:
>>
>> Greetings,
>>
>> On Fri, Feb 19, 2010 at 03:54:27PM -0500, Erik Lewis wrote:
>>
>>> I have to changes all the spaces in a string to +'s. =C2=A0Is there an
>>> easy way to do this. =C2=A0The length of the string and the number of
>>> spaces will always be changing.
>>
>> $string =3D~ s/ /\+/g;
>>
>> That is all. All spaces will be replaced.
>
> You don't have to escape a plus sign in a quoted string:
>
> $string =3D~ s/ /+/g;
>
> Or for a more efficient way:
>
> $string =3D~ tr/ /+/;
For example:
$ echo "foo bar zoo" | perl -pne '$_ =3D~ s/ /+/g'
foo+bar++zoo
$ echo "foo bar zoo" | perl -pne '$_ =3D~ y/ /+/'
foo+bar++zoo
Regards,
- Robert
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
From: Erik Lewis <elewis [at] ngrl.org>
> <snipped>
> print "Enter your address\n";
>
> chomp (my $rawaddress = <>);
>
> my $geoaddress = $rawaddress =~ s/ /\+/;
> #strip the spaces from the address
>
> my $googlekey = "ABQIAAAAJKeZa28YtErALcrbEC0UlBREf5oWR6F07BQvSEe3pww8R4s0VhT fTt-19vTI9qA-_V1pUf4-_TcfpQ";
> #get your google http://code.google.com/apis/maps/signup.html
>
> my $geocode_csv = get("http://maps.google.com/maps/geo?q=$geoaddress&output=cs v&sensor=false&key=$googlekey")
> or die 'Unable to get page';
> <snipped>
As if I did not think so. No you do not need to replace spaces by
plus signs! You need to escape the string for inclusion in a query
string!
use CGI::Enurl qw(enurl);
my $geoaddress = enurl($rawaddress);
or
use URI::Escape qw(uri_escape);
my $geoaddress = uri_escape($rawaddress);
Jenda
===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
Thanks that probably explains my higher than expected ungeocoded
rate. Two weeks of playing with perl and I feel like I know less than
when I started.
On Feb 25, 2010, at 3:31 AM, "Jenda Krynicky" <Jenda [at] Krynicky.cz> wrote:
> From: Erik Lewis <elewis [at] ngrl.org>
>> <snipped>
>> print "Enter your address\n";
>>
>> chomp (my $rawaddress = <>);
>>
>> my $geoaddress = $rawaddress =~ s/ /\+/;
>> #strip the spaces from the address
>>
>> my $googlekey =
>> "ABQIAAAAJKeZa28YtErALcrbEC0UlBREf5oWR6F07BQvSEe3pww8R4s0VhT fTt-
>> 19vTI9qA-_V1pUf4-_TcfpQ";
>> #get your google http://code.google.com/apis/maps/signup.html
>>
>> my $geocode_csv = get("http://maps.google.com/maps/geo?q=$geoaddress&output=cs v&sensor=false&key=$googlekey
>> ")
>> or die 'Unable to get page';
>> <snipped>
>
> As if I did not think so. No you do not need to replace spaces by
> plus signs! You need to escape the string for inclusion in a query
> string!
>
> use CGI::Enurl qw(enurl);
> my $geoaddress = enurl($rawaddress);
>
> or
>
> use URI::Escape qw(uri_escape);
> my $geoaddress = uri_escape($rawaddress);
>
>
> Jenda
> ===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
> When it comes to wine, women and song, wizards are allowed
> to get drunk and croon as much as they like.
> -- Terry Pratchett in Sourcery
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: how do I replace all the spaces in a string with +
From: Erik Lewis <elewis [at] ngrl.org>
> Thanks that probably explains my higher than expected ungeocoded
> rate. Two weeks of playing with perl and I feel like I know less than
> when I started.
The more you learn the more you find out you know nothing ;-)
This feeling is to be expected. Don't let it overwhelm you and go on
playing :-)
And while we are at it ...
Because they are all backwards!
Why is that?
Because it makes the posts hard to read.
Why?
Please do not top-post!
Jenda
===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/