Convert String to Hash

N

Nick Snels

Hi,

I get a string inputted in my program, which I convert to a hash-like
string, like:

sql = '"table_alias" => [],"original_string" => "select * from table"'

The string has the form of a hash, now I would like to convert this
string to an actual hash. I tried it like:

test = Hash[sql]
or
test = Hash.[](sql)

but I get the following error:
test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
from test2.rb:2

When I do it like,

test = Hash.new
test = sql

test becomes a String and is no longer a Hash.

Is there an easy way to convert my hash-like string to an actual hash?
Thanks for any help.

Kind regards,

Nick
 
B

Bernhard 'elven' Stoeckner

Nick Snels scribbled on Thursday 09 Mar 2006 12:52:
Hi,

I get a string inputted in my program, which I convert to a hash-like
string, like:

sql = '"table_alias" => [],"original_string" => "select * from table"'

The string has the form of a hash, now I would like to convert this
string to an actual hash. I tried it like:

test = Hash[sql]
or
test = Hash.[](sql)

but I get the following error:
test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
from test2.rb:2

When I do it like,

test = Hash.new
test = sql

test becomes a String and is no longer a Hash.

Is there an easy way to convert my hash-like string to an actual hash?
Thanks for any help.

Kind regards,

Nick

Hi Nick,

no, you can't pass it to Hash.new directly, you'd have to write a parser for
it yourself. If you're feeling lucky, you can use eval() to do that.

*takes cover from flying bananas and such*

Bernhard
 
R

Rob Pitt

*throws banana ad Bernhard*

no no no don't eval user supplied strings you are asking to be hacked :)

Nick Snels scribbled on Thursday 09 Mar 2006 12:52:
Hi,

I get a string inputted in my program, which I convert to a hash-like
string, like:

sql = '"table_alias" => [],"original_string" => "select * from
table"'

The string has the form of a hash, now I would like to convert this
string to an actual hash. I tried it like:

test = Hash[sql]
or
test = Hash.[](sql)

but I get the following error:
test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
from test2.rb:2

When I do it like,

test = Hash.new
test = sql

test becomes a String and is no longer a Hash.

Is there an easy way to convert my hash-like string to an actual
hash?
Thanks for any help.

Kind regards,

Nick

Hi Nick,

no, you can't pass it to Hash.new directly, you'd have to write a
parser for
it yourself. If you're feeling lucky, you can use eval() to do that.

*takes cover from flying bananas and such*

Bernhard
 
M

Marcin Mielżyński

Nick said:
Hi,

I get a string inputted in my program, which I convert to a hash-like
string, like:

sql = '"table_alias" => [],"original_string" => "select * from table"'

The simplest, although slow:

sql = '"table_alias" => [],"original_string" => "select * from table"'
hash = eval('{'+sql+'}')


lopex
 
B

Bernhard 'elven' Stoeckner

Rob Pitt scribbled on Thursday 09 Mar 2006 13:01:
*throws banana ad Bernhard*

no no no don't eval user supplied strings you are asking to be hacked :)

Well, in certain local-only low-security scenarios it might be of use. I
wouldn't know where Nick takes his strings from. :}
 
R

Rob Pitt

Argh!

Please stop telling people to eval user supplied strings!

The *ONLY* time it is safe to use eval is if all the contents are =20
generated by you and NONE of the components come from user input.

It looks here like original_string comes from user.

Imagine if I supplied this:

" =3D> 'lame'; `rm -rf /` #

Nick said:
Hi,
I get a string inputted in my program, which I convert to a hash-=20
like string, like:
sql =3D '"table_alias" =3D> [],"original_string" =3D> "select * from =20=
table"'

The simplest, although slow:

sql =3D '"table_alias" =3D> [],"original_string" =3D> "select * from = table"'
hash =3D eval('{'+sql+'}')


lopex
 
N

Nick Snels

Hi,

thanks for all the replies. I used the eval method and it worked!! The
string doesn't come directly from user input. It first goes through a
Perl sql parser and then into my program. So hopefully this doesn't
introduce any security issues, but I will be aware of the possibility.
Thanks for the help.

Kind regards,

Nick
 

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
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top