Help

J

Jagadeesh

hi Gurus,

Here my data structure

1234 => { 'open' => '1234-1',
'analyzed' => '1234-2',
'feedback' => '1234-3',
'closed' => '1234-4'
}
3455 => { 'open' => '3455-1',
'analyzed' => '34552',
'feedback' => '3455-3',
'closed' => '3455-4'
}

My task is to display numbers [like 1234 or 3455] if they have ('open'
OR 'analyzed') AND ('closed' OR 'feedback') keys. I tried following
code

puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
&& ('open' ||
'analyzed') ) )

I am not sure what is wrong with it but its not testing these
conditions and printing all numbers. Please help me understanding
has_key method and writing correct condition.

Thanks
 
J

Jesús Gabriel y Galán

hi Gurus,

Here my data structure

1234 =3D> { 'open' =3D> '1234-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '1234-2',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '1234-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '1234-4'
=A0 =A0 =A0 =A0 =A0 =A0 }
3455 =3D> { 'open' =3D> '3455-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '34552',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '3455-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '3455-4'
=A0 =A0 =A0 =A0 =A0 =A0 }

My task is to display numbers [like 1234 or 3455] if they have ('open'
OR 'analyzed') AND ('closed' OR 'feedback') =A0keys. I tried following
code

puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 && ('open' ||
'analyzed') ) )

I am not sure what is wrong with it but its not testing these
conditions and printing all numbers. Please help me understanding
has_key method and writing correct condition.

The has_key? method receives a single key to be checked in the hash.
In your snippet, what you are passing to the has_key? method is the
result of evaluating this:

( ('closed' || 'feedback') && ('open' || 'analyzed') )

which is:

irb(main):002:0> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
=3D> "open"

So you are testing only for the "open" key. I don't know what $DATA or
pr or numbers are in your snippet, but you are testing only for the
key "open".

Hope this helps,

Jesus.
 
S

Stefano Crocco

|hi Gurus,
|
|Here my data structure
|
|1234 => { 'open' => '1234-1',
| 'analyzed' => '1234-2',
| 'feedback' => '1234-3',
| 'closed' => '1234-4'
| }
|3455 => { 'open' => '3455-1',
| 'analyzed' => '34552',
| 'feedback' => '3455-3',
| 'closed' => '3455-4'
| }
|
|My task is to display numbers [like 1234 or 3455] if they have ('open'
|OR 'analyzed') AND ('closed' OR 'feedback') keys. I tried following
|code
|
|puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
| && ('open' ||
|'analyzed') ) )
|
|I am not sure what is wrong with it but its not testing these
|conditions and printing all numbers. Please help me understanding
|has_key method and writing correct condition.
|
|Thanks

Not tested, but I think the condition should be:

($DATA[pr].has_key?('closed') || $DATA[pr].has_key?('feedback')) &&
($DATA[pr].has_key?('open') || $DATA[pr].has_key?('analyzed'))

Your condition would always have translated to

if $DATA[pr].has_key?('open')

This happens because ruby first evaluates the argument to has_key? and because
of the way logical operators work. || returns the first operand which
evaluates to true (that is, the first operand which is not false or nil). This
means that:
'closed' || 'feedback' always gives 'closed'
'open' || 'analyzed' always gives 'open'

On the other hand, the && operator returns the last operand if both evaluate
to true and the value of the one which evaluates to false if one evaluates to
false (if both evaluate to false, it returns the first). In your case, you had
the expression

'closed' && 'open'

which evaluates to 'open'.

I hope this helps

Stefano
 
J

Jagadeesh

|hi Gurus,
|
|Here my data structure
|
|1234 => { 'open' => '1234-1',
|               'analyzed' => '1234-2',
|               'feedback' => '1234-3',
|               'closed' => '1234-4'
|             }
|3455 => { 'open' => '3455-1',
|               'analyzed' => '34552',
|               'feedback' => '3455-3',
|               'closed' => '3455-4'
|             }
|
|My task is to display numbers [like 1234 or 3455] if they have ('open'
|OR 'analyzed') AND ('closed' OR 'feedback')  keys. I tried following
|code
|
|puts numbers if   $DATA[pr].has_key?( ( ('closed' || 'feedback')
|                                                         && ('open' ||
|'analyzed') ) )
|
|I am not sure what is wrong with it but its not testing these
|conditions and printing all numbers. Please help me understanding
|has_key method and writing correct condition.
|
|Thanks

Not tested, but I think the condition should be:

($DATA[pr].has_key?('closed') || $DATA[pr].has_key?('feedback')) &&
($DATA[pr].has_key?('open') || $DATA[pr].has_key?('analyzed'))

Your condition would always have translated to

if $DATA[pr].has_key?('open')

This happens because ruby first evaluates the argument to has_key? and because
of the way logical operators work. || returns the first operand which
evaluates to true (that is, the first operand which is not false or nil).This
means that:
'closed' || 'feedback' always gives 'closed'
'open' || 'analyzed' always gives 'open'

On the other hand, the && operator returns the last operand if both evaluate
to true and the value of the one which evaluates to false if one evaluates to
false (if both evaluate to false, it returns the first). In your case, you had
the expression

'closed' && 'open'

which evaluates to 'open'.

I hope this helps

Stefano

Thank you so much for crystal clear explanation. Would you show me how
I can do it using array? I tried using grep but did not get good
result.

I want to print if I find (closed or feedback) and (open or analyzed)
keys.

Thanks
 
J

Jagadeesh

hi Gurus,
Here my data structure
1234 => { 'open' => '1234-1',
              'analyzed' => '1234-2',
              'feedback' => '1234-3',
              'closed' => '1234-4'
            }
3455 => { 'open' => '3455-1',
              'analyzed' => '34552',
              'feedback' => '3455-3',
              'closed' => '3455-4'
            }
My task is to display numbers [like 1234 or 3455] if they have ('open'
OR 'analyzed') AND ('closed' OR 'feedback')  keys. I tried following
code
puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
                                                       && ('open' ||
'analyzed') ) )
I am not sure what is wrong with it but its not testing these
conditions and printing all numbers. Please help me understanding
has_key method and writing correct condition.

The has_key? method receives a single key to be checked in the hash.
In your snippet, what you are passing to the has_key? method is the
result of evaluating this:

( ('closed' || 'feedback') && ('open' || 'analyzed') )

which is:

irb(main):002:0> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
=> "open"

So you are testing only for the "open" key. I don't know what $DATA or
pr or numbers are in your snippet, but you are testing only for the
key "open".

Hope this helps,

Jesus.

$DATA=>{'1234' => { 'open' => '1234-1',
'analyzed' => '1234-2',
'feedback' => '1234-3',
'closed' => '1234-4'
},
'3455' => { 'open' => '3455-1',
'analyzed' => '34552',
'feedback' => '3455-3',
'closed' => '3455-4'
}
}

and am reading it in loop so pr will have 1234, 3455 etc.

Thanks for your help.

Thanks
 
J

Jagadeesh

Here is how I am trying to do with array

states = ['open', 'analyzed', 'feedback', 'closed']

unless ( ( states.grep('closed').empty? || states.grep
('feedback').empty? ) \
&& (states.grep('open').empty? ||states.grep
('analyzed').empty? ) )
puts "hello"
end

Thanks
 
J

Jesús Gabriel y Galán

hi Gurus,
Here my data structure
1234 =3D> { 'open' =3D> '1234-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '1234-2',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '1234-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '1234-4'
=A0 =A0 =A0 =A0 =A0 =A0 }
3455 =3D> { 'open' =3D> '3455-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '34552',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '3455-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '3455-4'
=A0 =A0 =A0 =A0 =A0 =A0 }
My task is to display numbers [like 1234 or 3455] if they have ('open'
OR 'analyzed') AND ('closed' OR 'feedback') =A0keys. I tried following
code
puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 && ('open' ||
'analyzed') ) )
I am not sure what is wrong with it but its not testing these
conditions and printing all numbers. Please help me understanding
has_key method and writing correct condition.

The has_key? method receives a single key to be checked in the hash.
In your snippet, what you are passing to the has_key? method is the
result of evaluating this:

( ('closed' || 'feedback') && ('open' || 'analyzed') )

which is:

irb(main):002:0> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
=3D> "open"

So you are testing only for the "open" key. I don't know what $DATA or
pr or numbers are in your snippet, but you are testing only for the
key "open".

Hope this helps,

Jesus.

$DATA=3D>{'1234' =3D> { 'open' =3D> '1234-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '1234-2',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '1234-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '1234-4'
=A0 =A0 =A0 =A0 =A0 =A0 },
=A0 =A0 =A0 =A0'3455' =3D> { 'open' =3D> '3455-1',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '34552',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '3455-3',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '3455-4'
=A0 =A0 =A0 =A0 =A0 =A0 =A0}
}

and am reading it in loop so pr will have 1234, 3455 etc.

As Stefano said:

irb(main):001:0> $DATA =3D {
irb(main):002:1* 1234 =3D> { 'open' =3D> '1234-1',
irb(main):003:2* 'analyzed' =3D> '1234-2',
irb(main):004:2* 'feedback' =3D> '1234-3',
irb(main):005:2* 'closed' =3D> '1234-4'
irb(main):006:2> },
irb(main):007:1* 3455 =3D> { 'open' =3D> '3455-1',
irb(main):008:2* 'analyzed' =3D> '34552',
irb(main):009:2* 'feedback' =3D> '3455-3',
irb(main):010:2* 'closed' =3D> '3455-4'
irb(main):011:2> }}
=3D> {3455=3D>{"open"=3D>"3455-1", "analyzed"=3D>"34552", "closed"=3D>"3455=
-4",
"feedback"=3D>"3455-3"}, 1234=3D>{"open"=3D>"1234-1", "analyzed"=3D>"1234-2=
",
"closed"=3D>"1234-4", "feedback"=3D>"1234-3"}}
irb(main):012:0> $DATA.each do |k,v|
irb(main):013:1* puts k if ( (v.has_key?('closed') ||
v.has_key?('feedback')) && ((v.has_key?('open') ||
v.has_key?('analyzed'))))
irb(main):014:1> end
3455
1234


Hope this helps,

Jesus.
 
S

Stefano Crocco

|> On Thursday 07 January 2010, Jagadeesh wrote:
|> > |hi Gurus,
|> > |
|> > |Here my data structure
|> > |
|> > |1234 =3D> { 'open' =3D> '1234-1',
|> > |
|> > | 'analyzed' =3D> '1234-2',
|> > | 'feedback' =3D> '1234-3',
|> > | 'closed' =3D> '1234-4'
|> > | }
|> > |
|> > |3455 =3D> { 'open' =3D> '3455-1',
|> > |
|> > | 'analyzed' =3D> '34552',
|> > | 'feedback' =3D> '3455-3',
|> > | 'closed' =3D> '3455-4'
|> > | }
|> > |
|> > |My task is to display numbers [like 1234 or 3455] if they have
|> > |('open' OR 'analyzed') AND ('closed' OR 'feedback') keys. I tried
|> > |following code
|> > |
|> > |puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
|> > |
|> > | && ('open'
|> > | ||
|> > |
|> > |'analyzed') ) )
|> > |
|> > |I am not sure what is wrong with it but its not testing these
|> > |conditions and printing all numbers. Please help me understanding
|> > |has_key method and writing correct condition.
|> > |
|> > |Thanks
|>=20
|> Not tested, but I think the condition should be:
|>=20
|> ($DATA[pr].has_key?('closed') || $DATA[pr].has_key?('feedback')) &&
|> ($DATA[pr].has_key?('open') || $DATA[pr].has_key?('analyzed'))
|>=20
|> Your condition would always have translated to
|>=20
|> if $DATA[pr].has_key?('open')
|>=20
|> This happens because ruby first evaluates the argument to has_key? and
|> because of the way logical operators work. || returns the first operand
|> which evaluates to true (that is, the first operand which is not false
|> or nil). This means that:
|> 'closed' || 'feedback' always gives 'closed'
|> 'open' || 'analyzed' always gives 'open'
|>=20
|> On the other hand, the && operator returns the last operand if both
|> evaluate to true and the value of the one which evaluates to false if
|> one evaluates to false (if both evaluate to false, it returns the
|> first). In your case, you had the expression
|>=20
|> 'closed' && 'open'
|>=20
|> which evaluates to 'open'.
|>=20
|> I hope this helps
|>=20
|> Stefano
|
|Thank you so much for crystal clear explanation. Would you show me how
|I can do it using array? I tried using grep but did not get good
|result.
|
|I want to print if I find (closed or feedback) and (open or analyzed)
|keys.
|
|Thanks

I'm not sure about what you mean by "using array?". Your code clearly uses =
a=20
hash, and Array has no keys and, obviously, no has_key? method. Looking at=
=20
your reply to Jes=FAs, I'd say what you want is this:

$DATA.each_pair do |key, value|
if (value.has_key?('closed') || value.has_key?('feedback')) &&=20
(value.has_key?('open') || value.has_key?('analyzed'))
puts key=20
end
end

Hash#each_pair calls the block for each key-value pair. The first argument =
of=20
the block will be set to the key; the second will be set to the value.

If you're sure the values associated with the 'closed', 'feedback', 'open' =
and=20
'analyzed' keys will never be nil or false, you can replace the condition w=
ith=20
this:

if (value['closed'] || value['feedback']) && (value['open'] ||=20
value['analyzed'])

This way, instead of checking whether the hash has a given key, you check=20
whether or not the associated value is nil. Since Hash returns nil if you a=
sk=20
for a non existing key, you'll get a true value (in your case, a string) if=
=20
the key exists and nil if it doesn't. This means you get a true value when=
=20
has_key? would have returned true and a false value (nil) when has_key? wou=
ld=20
have returned false. This, of course, breaks if a key can correspond to a=20
value which is false or nil.

Stefano
 
S

Stefano Crocco

|Here is how I am trying to do with array
|
|states = ['open', 'analyzed', 'feedback', 'closed']
|
| unless ( ( states.grep('closed').empty? || states.grep
|('feedback').empty? ) \
| && (states.grep('open').empty? ||states.grep
|('analyzed').empty? ) )
| puts "hello"
| end
|
|Thanks

Still I don't understand exactly what you're trying to do. states contains all
the four entries, so states.grep will never return an empty array and the puts
will always be executed.

Please, can you explain in detail what you're trying to do? This way we'll be
able to help you better.

Stefano
 
S

Stefano Crocco

|> On Thursday 07 January 2010, Jagadeesh wrote:
|> > |Here is how I am trying to do with array
|> > |
|> > |states = ['open', 'analyzed', 'feedback', 'closed']
|> > |
|> > | unless ( ( states.grep('closed').empty? || states.grep
|> > |
|> > |('feedback').empty? ) \
|> > |
|> > | && (states.grep('open').empty? ||states.grep
|> > |
|> > |('analyzed').empty? ) )
|> > |
|> > | puts "hello"
|> > | end
|> > |
|> > |Thanks
|>
|> Still I don't understand exactly what you're trying to do. states
|> contains all the four entries, so states.grep will never return an
|> empty array and the puts will always be executed.
|>
|> Please, can you explain in detail what you're trying to do? This way
|> we'll be able to help you better.
|>
|> Stefano
|
|Actually there may be an arry without all those values. So I extracted
|keys into an array and was trying to grep in it.
|
|Thanks for your help.
|
|Thanks

You don't need to use grep to see whether an array contains an element. You
can use include?:

if (states.include?('closed') || states.include?('feedback')) &&
(states.include?('open') || states.include?('analyzed'))

Another aopproach is to use the Array & operator, which gives the intersection
between two arrays. You can do something like this:

first_group = ['open', 'analyzed']
second_group = ['closed', 'feedback']

if !(states & second_group).empty? && !(states & first_group).empty?

The reasoning is that, if states contains either the 'open' or the 'analyzed'
entries, then its intersection with first_group (states & first_group) won't
be empty. The same for the second group.

I hope this helps

Stefano
 
J

Jagadeesh

hi Gurus,
Here my data structure
1234 => { 'open' => '1234-1',
              'analyzed' => '1234-2',
              'feedback' => '1234-3',
              'closed' => '1234-4'
            }
3455 => { 'open' => '3455-1',
              'analyzed' => '34552',
              'feedback' => '3455-3',
              'closed' => '3455-4'
            }
My task is to display numbers [like 1234 or 3455] if they have ('open'
OR 'analyzed') AND ('closed' OR 'feedback')  keys. I tried following
code
puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
                                                        && ('open' ||
'analyzed') ) )
I am not sure what is wrong with it but its not testing these
conditions and printing all numbers. Please help me understanding
has_key method and writing correct condition.
The has_key? method receives a single key to be checked in the hash.
In your snippet, what you are passing to the has_key? method is the
result of evaluating this:
( ('closed' || 'feedback') && ('open' || 'analyzed') )
which is:
irb(main):002:0> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
=> "open"
So you are testing only for the "open" key. I don't know what $DATA or
pr or numbers are in your snippet, but you are testing only for the
key "open".
Hope this helps,
Jesus.
$DATA=>{'1234' => { 'open' => '1234-1',
              'analyzed' => '1234-2',
              'feedback' => '1234-3',
              'closed' => '1234-4'
            },
       '3455' => { 'open' => '3455-1',
              'analyzed' => '34552',
              'feedback' => '3455-3',
              'closed' => '3455-4'
             }
}
and am reading it in loop so pr will have 1234, 3455 etc.

As Stefano said:

irb(main):001:0> $DATA = {
irb(main):002:1* 1234 => { 'open' => '1234-1',
irb(main):003:2*               'analyzed' => '1234-2',
irb(main):004:2*               'feedback' => '1234-3',
irb(main):005:2*               'closed' => '1234-4'
irb(main):006:2>             },
irb(main):007:1* 3455 => { 'open' => '3455-1',
irb(main):008:2*               'analyzed' => '34552',
irb(main):009:2*               'feedback' => '3455-3',
irb(main):010:2*               'closed' => '3455-4'
irb(main):011:2>             }}
=> {3455=>{"open"=>"3455-1", "analyzed"=>"34552", "closed"=>"3455-4",
"feedback"=>"3455-3"}, 1234=>{"open"=>"1234-1", "analyzed"=>"1234-2",
"closed"=>"1234-4", "feedback"=>"1234-3"}}
irb(main):012:0> $DATA.each do |k,v|
irb(main):013:1* puts k if ( (v.has_key?('closed') ||
v.has_key?('feedback')) && ((v.has_key?('open') ||
v.has_key?('analyzed'))))
irb(main):014:1> end
3455
1234

Hope this helps,

Jesus.

Thank you so much. It is working now.

Thanks for all your help.

Thanks
 

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
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top