Array of hashes issue

B

Brad Dude

Hi,

I'm new to ruby and am trying to do a project so I can learn ruby and
build a tool I need for my job.

I want to creat an array of hashes and then get the information out.

This is what I have right now:

this is in my getIssuesInFileter class:
@issues = @jira.getIssuesFromFilter(@key)
@output = Array.new(@issues.length, Hash.new)
@issues.each {
|fl|
@output << {:id => fl.id, :assignee => fl.assignee, :description
=> fl.description, :status => fl.status, :type => fl.type, :updated =>
fl.updated}
}
return @output

This is where I want to output my results
issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each {
|iss|
iss.each {
|issSecD|
puts issSecD[:description], "\n"
}
}


I've tried to set up the hash like this {'id' => fl.id, ......

and then address it like this puts issSecD['id'], "\n"

But, I keep getting errors. Tried to find answers on google. Got
frustrated so I figured I'd ask the board.

Thanks in advance.
 
J

Jesús Gabriel y Galán

I'm new to ruby and am trying to do a project so I can learn ruby and
build a tool I need for my job.

Hi, welcome !
I want to creat an array of hashes and then get the information out.

This is what I have right now:

this is in my getIssuesInFileter class:
=A0 =A0 =A0@issues =3D @jira.getIssuesFromFilter(@key)
=A0 =A0 =A0@output =3D Array.new(@issues.length, Hash.new)

First thing: I think this is incorrect, because you start with a
filled array of length @issues.length, all positions pointing to an
empty Hash. Check this:

irb(main):001:0> a =3D Array.new(10, Hash.new)
=3D> [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
irb(main):002:0> a << {:a =3D> 3}
=3D> [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {:a=3D>3}]

You see, when I add to the array, I still have 10 positions pointing
to an empty hash. Don't think this is what you want.
I would do just:

@output =3D [] # or Array.new

=A0 =A0 [email protected] {
=A0 =A0 =A0 =A0|fl|
=A0 =A0 =A0 =A0@output << {:id =3D> fl.id, :assignee =3D> fl.assignee, :d= escription
=3D> fl.description, :status =3D> fl.status, :type =3D> fl.type, :updated= =3D>
fl.updated}
=A0 =A0 =A0}
return @output

When things don't go as expected, you can inspect the variables you
are dealing with, to see what's going on:
This is where I want to output my results
=A0 =A0 =A0 =A0issueArray =3D @JiraClass.getIssuesInFilter(newFilterVal[0= ])
=A0 =A0 =A0issueArray.each {
=A0 =A0 =A0 =A0|iss| p iss
=A0 =A0 =A0 =A0iss.each {
=A0 =A0 =A0 =A0 =A0|issSecD| p issSecD
=A0 =A0 =A0 =A0 =A0puts issSecD[:description], "\n"
=A0 =A0 =A0 =A0}
=A0 =A0 =A0}


Adding those two lines should give you a clue of what's going on: iss
is one of the hashes in the array. issSecD is an array with each
[key,value]. If you are only interested in the description:

issueArray =3D @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
puts issue[:description] #puts already prints a "\n". don't know if
you actually want 2 of them.
end

If you want to iterate all of the keys:

issueArray =3D @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
issue.each do |key, value|
puts "#{key} =3D> #{value}"
end

Hope this helps,

Jesus.
 
B

Brad Dude

Jesús Gabriel y Galán said:
@output = [] # or Array.new

� � �@issues.each {
� � � �|fl|
� � � �@output << {:id => fl.id, :assignee => fl.assignee, :description
=> fl.description, :status => fl.status, :type => fl.type, :updated =>
fl.updated}
� � �}
return @output

When things don't go as expected, you can inspect the variables you
are dealing with, to see what's going on:
This is where I want to output my results
� � � �issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])
� � �issueArray.each {
� � � �|iss| p iss
� � � �iss.each {
� � � � �|issSecD| p issSecD
� � � � �puts issSecD[:description], "\n"
� � � �}
� � �}


Adding those two lines should give you a clue of what's going on: iss
is one of the hashes in the array. issSecD is an array with each
[key,value]. If you are only interested in the description:

issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
puts issue[:description] #puts already prints a "\n". don't know if
you actually want 2 of them.
end

If you want to iterate all of the keys:

issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
issue.each do |key, value|
puts "#{key} => #{value}"
end

Hope this helps,

Jesus.



Hi,

This was very helpful and worked perfectly for me. But, I'm still not
there on what I want to do. I should have explained better. So, now that
I have a good hash with my information into it I want to pass it to
another method. If I were doing this in PHP or something it would look
like this:

myFunction(issueHash['id'], issueHash['description'],....)

So I guess what I'm missing is how do get my values out using the key?
 
J

Jesús Gabriel y Galán

Jes=C3=BAs Gabriel y Gal=C3=A1n said:
@output =3D [] # or Array.new

=EF=BF=BD =EF=BF=BD [email protected] {
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD|fl|
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD@output << {:id =3D> fl.id, :ass= ignee =3D> fl.assignee, :description
=3D> fl.description, :status =3D> fl.status, :type =3D> fl.type, :updat= ed =3D>
fl.updated}
=EF=BF=BD =EF=BF=BD =EF=BF=BD}
return @output

When things don't go as expected, you can inspect the variables you
are dealing with, to see what's going on:
This is where I want to output my results
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BDissueArray =3D @JiraClass.getIss= uesInFilter(newFilterVal[0])
=EF=BF=BD =EF=BF=BD =EF=BF=BDissueArray.each {
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD|iss| p iss
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BDiss.each {
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD|issSecD| p issSecD
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BDputs issSecD[:descript= ion], "\n"
=EF=BF=BD =EF=BF=BD =EF=BF=BD =EF=BF=BD}
=EF=BF=BD =EF=BF=BD =EF=BF=BD}


Adding those two lines should give you a clue of what's going on: iss
is one of the hashes in the array. issSecD is an array with each
[key,value]. If you are only interested in the description:

issueArray =3D @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
=C2=A0 puts issue[:description] =C2=A0#puts already prints a "\n". don't= know if
you actually want 2 of them.
end

If you want to iterate all of the keys:

issueArray =3D @JiraClass.getIssuesInFilter(newFilterVal[0])
issueArray.each do |issue|
=C2=A0 issue.each do |key, value|
=C2=A0 =C2=A0 puts "#{key} =3D> #{value}"
end

Hope this helps,

Jesus.



Hi,

This was very helpful and worked perfectly for me. But, I'm still not
there on what I want to do. I should have explained better. So, now that
I have a good hash with my information into it I want to pass it to
another method. If I were doing this in PHP or something it would look
like this:

myFunction(issueHash['id'], issueHash['description'],....)

So I guess what I'm missing is how do get my values out using the key?

I think you are setting the hash with symbols as keys, so you retrieve
them passing the hash the appropriate key:

issueHash[:id]
issueHash[:description]
...

(I'm not sure if this is the problem you are facing right now)

Jesus.
 
B

Brad Dude

Hey Jesus,

You led me in the right direction. Thanks!

I ended up having to do it like this:

issue.fetch("description")

Wish there was an easier way to get a value out of a hash by key.
Someone on the board might know.
 
D

Douglas Seifert

[Note: parts of this message were removed to make it a legal post.]
Wish there was an easier way to get a value out of a hash by key.
Someone on the board might know.


Getting values out of a hash by key is easy. I think the issue you were
having is you were unclear exactly what the key was and maybe were confused
about the difference between a Symbol and String in Ruby. A Symbol is kind
of like an interned String (to borrow some Java speak). They are separate
classes and both may be used as hash keys. So this works:

h = {}
h[:key] = 'my key is a Symbol'
h['key'] = 'my key is a String'

p h[:key]
"my key is a Symbol"
p h['key']
"my key is a String"

Getting a value out of the hash is as simple as it is in PHP -- just use the
[] method.

-Doug Seifert
 
B

Brad Dude

h = {}
h[:key] = 'my key is a Symbol'
h['key'] = 'my key is a String'

p h[:key]
"my key is a Symbol"
p h['key']
"my key is a String"

Getting a value out of the hash is as simple as it is in PHP -- just use
the
[] method.

-Doug Seifert

Thanks Doug! I'll give it a shot. Looks easy enough. You're right, I
didn't get the difference between symbol and string.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top