[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to add string objects to hash?

dare ruby

2/5/2008 7:07:00 AM

Dear friends,

I have created a string buffer.

I have read character by character from buffer using a method and stored
in a string object
like,

@test = String.new

The method iteration process continues till @flag=1. So for every
iteration of method a string object(@test) is returned from the method.

Now i want to move this returned string object @test into a hash. How
can i move string objects into hash.

I need to store all strings that are returned(@test) till flag=1 in a
hash. I dont want to use an array to store the values.

Please help me to do this process.

Thanks in advance

Regards,
Jose Martin
--
Posted via http://www.ruby-....

8 Answers

7stud --

2/5/2008 9:00:00 AM

0

dare ruby wrote:
> Dear friends,
>
> I have created a string buffer.
>
> I have read character by character from buffer using a method and stored
> in a string object
> like,
>
> @test = String.new
>
> The method iteration process continues till @flag=1. So for every
> iteration of method a string object(@test) is returned from the method.
>
> Now i want to move this returned string object @test into a hash. How
> can i move string objects into hash.
>
> I need to store all strings that are returned(@test) till flag=1 in a
> hash. I dont want to use an array to store the values.
>
> Please help me to do this process.
>
> Thanks in advance
>
> Regards,
> Jose Martin

This is an example of a hash:

h = {"a"=>"circle", "b"=>"square", "c"=>"triangle"}
puts h["a"]

--output:--
circle

Note that every element of a hash is a key/value pair. You say, "I want
to put my strings into a hash", but the elements of a hash are not
single objects--the elements of a hash are pairs of objects. If your
strings are to be the values, what do you want to be the keys?

--
Posted via http://www.ruby-....

dare ruby

2/5/2008 9:17:00 AM

0

Thank you very much for your information. what you said is right. I want
to put my strings into hash. But two objects are required for hash. Is
it possibble to use hashcode method to be used as key and string as a
value.

If its possible please suggest a way to do so because i need to put my
strings into a hash.

Thanks in advance..
>
> This is an example of a hash:
>
> h = {"a"=>"circle", "b"=>"square", "c"=>"triangle"}
> puts h["a"]
>
> --output:--
> circle
>
> Note that every element of a hash is a key/value pair. You say, "I want
> to put my strings into a hash", but the elements of a hash are not
> single objects--the elements of a hash are pairs of objects. If your
> strings are to be the values, what do you want to be the keys?

--
Posted via http://www.ruby-....

Todd Benson

2/6/2008 9:45:00 PM

0

On Feb 5, 2008 1:07 AM, dare ruby <martin@angleritech.com> wrote:
> Dear friends,
>
> I have created a string buffer.
>
> I have read character by character from buffer using a method and stored
> in a string object
> like,
>
> @test = String.new
>
> The method iteration process continues till @flag=1. So for every
> iteration of method a string object(@test) is returned from the method.
>
> Now i want to move this returned string object @test into a hash. How
> can i move string objects into hash.
>
> I need to store all strings that are returned(@test) till flag=1 in a
> hash. I dont want to use an array to store the values.
>
> Please help me to do this process.
>
> Thanks in advance
>
> Regards,
> Jose Martin

Just out of curiosity, what are your keys for the values? In other
words if I have "hello" and "goodbye", what key do you use to
correspond to "hello" and what key for "goodbye" that would require a
Hash object?

Todd

Todd Benson

2/6/2008 9:48:00 PM

0

On Feb 5, 2008 3:17 AM, dare ruby <martin@angleritech.com> wrote:
> Thank you very much for your information. what you said is right. I want
> to put my strings into hash. But two objects are required for hash. Is
> it possibble to use hashcode method to be used as key and string as a
> value.
>
> If its possible please suggest a way to do so because i need to put my
> strings into a hash.

The big question is "why?" It would be helpful if you could describe
your purpose. I don't see any value in using the #hash function as a
key.

Todd

7stud --

2/6/2008 9:54:00 PM

0

Todd Benson wrote:
> On Feb 5, 2008 3:17 AM, dare ruby <martin@angleritech.com> wrote:
>> Thank you very much for your information. what you said is right. I want
>> to put my strings into hash. But two objects are required for hash. Is
>> it possibble to use hashcode method to be used as key and string as a
>> value.
>>
>> If its possible please suggest a way to do so because i need to put my
>> strings into a hash.
>
> The big question is "why?" It would be helpful if you could describe
> your purpose. I don't see any value in using the #hash function as a
> key.
>
> Todd

Yeah, you're just creating a hash that looks like this:

h = {'hello'=>'hello', 'goodbye'=>'goodbye'}

--
Posted via http://www.ruby-....

7stud --

2/6/2008 9:57:00 PM

0

7stud -- wrote:
>
> Yeah, you're just creating a hash that looks like this:
>
> h = {'hello'=>'hello', 'goodbye'=>'goodbye'}

...and to retrieve a value from the hash, you would write:

x = 'hello'
puts h[x]

but you could just write:

puts x

and dispense with the hash altogether.

--
Posted via http://www.ruby-....

Robert Klemme

2/6/2008 10:12:00 PM

0

On 05.02.2008 08:07, dare ruby wrote:
> I have created a string buffer.

What exactly do you mean by that? There is String and StringIO - why
create another class that can hold strings?

> I have read character by character from buffer using a method and stored
> in a string object
> like,
>
> @test = String.new

This does not store anything in a String object. It just creates a new
empty String.

> The method iteration process continues till @flag=1. So for every
> iteration of method a string object(@test) is returned from the method.

Why? What is the purpose of all this?

> Now i want to move this returned string object @test into a hash. How
> can i move string objects into hash.

What exactly do you mean by "move"? You can put Strings into a Hash -
as keys and values. But why?

> I need to store all strings that are returned(@test) till flag=1 in a
> hash. I dont want to use an array to store the values.

Why not an Array? A Hash will loose the insertion order.

> Please help me to do this process.

Please provide enough information that someone can come up with proper
suggestions.

Regards

robert

dare ruby

2/8/2008 3:45:00 AM

0

Dear every one,

The following code is a sample code i tried to work out my task.

class Sample

def initialize

@test = String.new

end

def calculate!

if @character == 'C'

string_manipulate!

end

end

def string_manipulate

while @character != 'A'

@character = read_character! # method to read character from
buffer

@test << @character

end

if @character == 'A'

calculate!

else

end

@test

end
end

I need to write a method now to retrieve @test string from
string_manipulate method and store in a hash. Each time a @test string
is returned it should be stored in a hash not in an array. I have used
array to store it but now my task need to do it using hash.

so could anyone help me out to write a method to retrieve string objects
using a hash.

Thanks in advance......

Regards,
Jose Martin
--
Posted via http://www.ruby-....