Howto Delete 3 Leftmost Characters

S

Skeets

i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.
 
W

William James

Skeets said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

irb(main):001:0> s="#ip 127.0.0.1"
=> "#ip 127.0.0.1"
irb(main):002:0> s.slice!(0,3)
=> "#ip"
irb(main):003:0> s
=> " 127.0.0.1"
 
R

Robert Klemme

Skeets said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

If you do the grepping in Ruby you can do something like this:

ip = nil

File.foreach("foo.txt") do |line|
ip = $1 if /^#ip\s+(\d+(?:\.\d+){3})/ =~ line
end

HTH

robert
 
E

Esteban Manchado Velázquez

--aVD9QWMuhilNxW9f
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.
=20
i grep a file and it returns the following string:
=20
#ip 127.0.0.1
=20
i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.
=20
however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

"#ip 127.0.0.1"[3..-1] # =3D> " 127.0.0.1"
"#ip 127.0.0.1"[4..-1] # =3D> "127.0.0.1"

But you're probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # =3D> "127.0.0.1"

That is, "remove, from the beginning of the line, '#ip' followed by one or
more space characters (be them spaces, tabs or whatever)". If you don't know
regular expressions, _and_ you're into text processing, I recommend you to =
go
and read some book about regular expressions and practice a little (under
Linux there are a couple of handy utilities for that; I'm sure there must be
also for other platforms).

Regards,

--=20
Esteban Manchado Vel=E1zquez <[email protected]> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es

--aVD9QWMuhilNxW9f
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEzSnThYgK5b1UDsERAhvxAKCBujlym/pqzSmCxF8uBrJUSj9o6gCfXdO5
zpB70fF84zLjRveAZ5UnEG0=
=4jfG
-----END PGP SIGNATURE-----

--aVD9QWMuhilNxW9f--
 
S

Skeets

William said:
irb(main):001:0> s="#ip 127.0.0.1"
=> "#ip 127.0.0.1"
irb(main):002:0> s.slice!(0,3)
=> "#ip"
irb(main):003:0> s
=> " 127.0.0.1"

William, thanks. when i follow your approach, i get your result via
irb. however, i get a different result when i run code in a file. i'm
doing something different, but i don't know what.

here is the code:

#!/usr/bin/env ruby

if File.exist?( "ip.txt" )

f = File.open("ip.txt').grep(/#ip/)
(f.to_s).slice!(0,3)
# f = f.strip

end

puts f # this outputs "#ip 127.0.0.1" - i was expecting it to output
"127.0.0.1"

if i have

f = (f.to_s).slice!(0,3)

instead fo

(f.to_s).slice!(0,3)

then "puts f" prints "#ip"

tia...
 
D

dblack

Hi --

William, thanks. when i follow your approach, i get your result via
irb. however, i get a different result when i run code in a file. i'm
doing something different, but i don't know what.

here is the code:

#!/usr/bin/env ruby

if File.exist?( "ip.txt" )

f = File.open("ip.txt').grep(/#ip/)
(f.to_s).slice!(0,3)
# f = f.strip

end

puts f # this outputs "#ip 127.0.0.1" - i was expecting it to output
"127.0.0.1"

if i have

f = (f.to_s).slice!(0,3)

instead fo

(f.to_s).slice!(0,3)

then "puts f" prints "#ip"

The reason is that when you do this:

f.to_s.slice! ...

you're operating on a string object other than f. Ruby first does
f.to_s, which returns a new string, then does an in-place slice!
operation on that string.

In the f = ... example, you're reassigning to f, so now f is the new,
modified string.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
C

Chad Perrin

=20
But you're probably better off using regular expressions instead of fix= ed
indices:
=20
"#ip 127.0.0.1".sub(/^#ip\s+/, '') # =3D> "127.0.0.1"
=20
That is, "remove, from the beginning of the line, '#ip' followed by one= or
more space characters (be them spaces, tabs or whatever)". If you don't= know
regular expressions, _and_ you're into text processing, I recommend you= to go
and read some book about regular expressions and practice a little (und= er
Linux there are a couple of handy utilities for that; I'm sure there mu= st be
also for other platforms).

Actually, the best text I've ever had the pleasure to read for learning
regex syntax from scratch was Learning Perl. The downside is that you
kinda have to know some Perl for it to make much sense (or go through
the entire book and learn regular expressions as a part of learning
Perl) -- which is only a downside if you don't want to learn Perl, of
course. It's a reasonably easy book to work through, though, and great
at what it does. All else being equal, it's better to know more
languages anyway.

If there are some really good materials for regexen that are specific to
Ruby, of course, that may be more what you need -- but I don't know of
any off the top of my head. Obviously, several of the available e-books
and tutorials you can find online address regex usage, but as far as
I've seen they don't tend to do as good a job of it as Learning Perl.

--=20
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes necessa=
ry
to shoot the engineers and begin production." - MacUser, November 1990
 
S

Skeets

Esteban said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

"#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
"#ip 127.0.0.1"[4..-1] # => "127.0.0.1"

But you're probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"

That is, "remove, from the beginning of the line, '#ip' followed by one or
more space characters (be them spaces, tabs or whatever)". If you don't know
regular expressions, _and_ you're into text processing, I recommend you to go
and read some book about regular expressions and practice a little (under
Linux there are a couple of handy utilities for that; I'm sure there mustbe
also for other platforms).

Regards,

Esteban, and all - thank you.

this did the trick...

if File.exist?( 'current_ip.txt' )

f = File.open('current_ip.txt').grep(/#ip/)
f = f[0].sub(/^#ip\s+/, '')

end

puts f

the File.open grep line returned an array. i had to sort that out
first. i know this file will always have only one instance of #ip - is
there any way to force grep to return as a variable instead of an
array?

also, i believe the regex Esteban gave will get rid of all white spaces
- both to the left and right of the ip address.

thanks to everyone for the help.
 
S

Skeets

also, i believe the regex Esteban gave will get rid of all white spaces
- both to the left and right of the ip address.

actually, this isn't true so i still have to use the strip method.

i have some regex experience - but not too much.

i pretty much take some pain killers and then try and figure out what i
need as the need arises. ;-)

i'll bet this regex can be update to remove all spaces, however, i will
have to look into that next week.

thanks to everyone for getting me back on track.

i'm working on my first ruby program - and i'm impressed with ruby.

lots to learn, though.
 
W

William James

Skeets said:
Esteban said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

"#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
"#ip 127.0.0.1"[4..-1] # => "127.0.0.1"

But you're probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"

That is, "remove, from the beginning of the line, '#ip' followed by oneor
more space characters (be them spaces, tabs or whatever)". If you don'tknow
regular expressions, _and_ you're into text processing, I recommend youto go
and read some book about regular expressions and practice a little (under
Linux there are a couple of handy utilities for that; I'm sure there must be
also for other platforms).

Regards,

Esteban, and all - thank you.

this did the trick...

if File.exist?( 'current_ip.txt' )

f = File.open('current_ip.txt').grep(/#ip/)
f = f[0].sub(/^#ip\s+/, '')

end

puts f

the File.open grep line returned an array. i had to sort that out
first. i know this file will always have only one instance of #ip - is
there any way to force grep to return as a variable instead of an
array?

also, i believe the regex Esteban gave will get rid of all white spaces
- both to the left and right of the ip address.

thanks to everyone for the help.

file_name = 'current_ip.txt'
if File.exist?( file_name )
ip = File.read( file_name )[ /^#ip\s+([\d.]+)/, 1 ]
end
p ip
 
S

Schüle Daniel

Skeets said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

irb(main):022:0> "abcdeg".sub(/.../, "")
=> "deg"
irb(main):023:0> "abcdeg"[3..-1]
=> "deg"
irb(main):024:0>

hth, Daniel
 
S

Skeets

Schüle Daniel said:
Skeets said:
i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

irb(main):022:0> "abcdeg".sub(/.../, "")
=> "deg"
irb(main):023:0> "abcdeg"[3..-1]
=> "deg"
irb(main):024:0>

hth, Daniel

thanks everyone - this whole discussion has been enlightening for this
newb.
 
J

Jeff Pritchard

Esteban said:
...snip...
That is, "remove, from the beginning of the line, '#ip' followed by one
or
more space characters (be them spaces, tabs or whatever)".>
...snip...
Regards,

Reading this explanation of the regexp that was provided reminded me
again of a question that has been haunting me for awhile. Has anyone
written an "English language to Regexp" converter?

It seems to me that for relatively simple cases it should be relatively
easy to set up something similar to the way most email apps do rule
building for filtering messages, with a drop down list of "contains,
starts with, doesn't contain, ends with, ...) and a couple of buttons
for adding/removing another line of such stuff. Sort of a wizard for
building RegExp.

Unless you write two or three RegExp's a day, it seems nearly impossible
to remember this arcane but useful syntax. Being able to write them in
plain English or build one with some simple drop downs and check boxes
etc. would certainly save me some pain on occasion. I've searched for
something like this before but never found anything.

jp
 
T

Timothy Hunter

Jeff said:
Reading this explanation of the regexp that was provided reminded me
again of a question that has been haunting me for awhile. Has anyone
written an "English language to Regexp" converter?

It seems to me that for relatively simple cases it should be relatively
easy to set up something similar to the way most email apps do rule
building for filtering messages, with a drop down list of "contains,
starts with, doesn't contain, ends with, ...) and a couple of buttons
for adding/removing another line of such stuff. Sort of a wizard for
building RegExp.

Unless you write two or three RegExp's a day, it seems nearly impossible
to remember this arcane but useful syntax. Being able to write them in
plain English or build one with some simple drop downs and check boxes
etc. would certainly save me some pain on occasion. I've searched for
something like this before but never found anything.

jp

Sounds like a Ruby Quiz to me!


However, I've used a variety of these GUI-based regex builders in the
past and never found them to much more than a curiosity. The regular
expression syntax is so precise and exact that I find it frustrating to
have to pick through all the choices and click things. Invariably I end
up just writing the damn regex by hand. I would think that an English
language equivalent to regex syntax would simply replace
hard-to-remember regular expressions with hard-to-remember English phrases.

Regular expressions are no harder to learn than any other programming
language. You need a good reference (for me it has to be on paper) and
some practice. That's all.
 
D

dblack

Hi --

Reading this explanation of the regexp that was provided reminded me
again of a question that has been haunting me for awhile. Has anyone
written an "English language to Regexp" converter?

Yes, Florian Gross has, though I don't know how complete it is (this
was a couple of years ago and I haven't heard much about it since
then). It's called Regexp::English. I can't seem to find it
anywhere... paging Florian....


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Martin DeMello

But you're probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # =3D> "127.0.0.1"

A prettier looking form, in this case, is

s =3D "#ip 127.0.0.1"
s[/^#ip /] =3D ''

martin
 
C

Chad Perrin

But you're probably better off using regular expressions instead of fi= xed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # =3D> "127.0.0.1"
=20
A prettier looking form, in this case, is
=20
s =3D "#ip 127.0.0.1"
s[/^#ip /] =3D ''

That changes the behavior of the regex, though. Maybe more like:

s =3D "#ip 127.0.0.1"
s[/^#ip\s+/] =3D ''

. . unless we're sure there will always be exactly one space (which is
the case if 's =3D "#ip 127.0.0.1"' is the literal code we're using), in
which case the literal space in the matching regex is appropriate.

--=20
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the Lucid
 
E

Esteban Manchado Velázquez

--k+w/mQv8wyuph6w0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
actually, this isn't true so i still have to use the strip method.
=20
i have some regex experience - but not too much.
=20
i pretty much take some pain killers and then try and figure out what i
need as the need arises. ;-)
=20
i'll bet this regex can be update to remove all spaces, however, i will
have to look into that next week.

Something like:

s =3D "#ip 127.0.0.1 "
s.sub(/^#ip\s+(.+?)\s*$/, '\1')

That's way uglier, because you have to match the whole line and then
substitute everything for the first parens contents.... _and_ you have to u=
se
".+?" instead of simply ".+" inside them, or otherwise you will get everyth=
ing
until the end of the line :-/

The "?" after "*" or "+" means non-greediness, BTW.

--=20
Esteban Manchado Vel=E1zquez <[email protected]> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es

--k+w/mQv8wyuph6w0
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEziPfhYgK5b1UDsERAohiAJ9bMlzyZJma2fdf0MbXh0IKypuh1gCfUqGr
Cq9eDHOvO7La5Ur52CHYDYM=
=ho+Q
-----END PGP SIGNATURE-----

--k+w/mQv8wyuph6w0--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top