[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

numbers of a record in an array

Bu Mihai

3/25/2008 9:09:00 AM

a have an array array=["a","b","c","a","b"]
how can i find the numbers of "a", "b","c" items in the array?
--
Posted via http://www.ruby-....

13 Answers

Jesús Gabriel y Galán

3/25/2008 9:27:00 AM

0

On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wrote:
> a have an array array=["a","b","c","a","b"]
> how can i find the numbers of "a", "b","c" items in the array?

Here's one way:

irb(main):006:0> a = %w{a b a c b a c c}
=> ["a", "b", "a", "c", "b", "a", "c", "c"]
irb(main):007:0> h = Hash.new {|h,k| h[k] = 0}
=> {}
irb(main):008:0> a.each {|x| h[x] += 1}
=> ["a", "b", "a", "c", "b", "a", "c", "c"]
irb(main):009:0> h
=> {"a"=>3, "b"=>2, "c"=>3}

Hope this helps,

Jesus.

Robert Klemme

3/25/2008 10:24:00 AM

0

2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wrote=
:
> > a have an array array=3D["a","b","c","a","b"]
> > how can i find the numbers of "a", "b","c" items in the array?
>
>
> Here's one way:
>
> irb(main):006:0> a =3D %w{a b a c b a c c}
> =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> irb(main):007:0> h =3D Hash.new {|h,k| h[k] =3D 0}
> =3D> {}
> irb(main):008:0> a.each {|x| h[x] +=3D 1}
> =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> irb(main):009:0> h
> =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}

Here's another

irb(main):001:0> a =3D %w{a b a c b a c c}
=3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=3D1; cnt}
=3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}

Jes=FAs, note that you do not need the block form of Hash.new because of
the way + works. It is sufficient to make 0 the default element.

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end

Jesús Gabriel y Galán

3/25/2008 12:38:00 PM

0

On Tue, Mar 25, 2008 at 11:23 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> > On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wro=
te:
> > > a have an array array=3D["a","b","c","a","b"]
> > > how can i find the numbers of "a", "b","c" items in the array?
> > irb(main):006:0> a =3D %w{a b a c b a c c}
> > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> > irb(main):007:0> h =3D Hash.new {|h,k| h[k] =3D 0}
> > =3D> {}
> > irb(main):008:0> a.each {|x| h[x] +=3D 1}
> > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> > irb(main):009:0> h
> > =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
>
> Here's another
>
> irb(main):001:0> a =3D %w{a b a c b a c c}
>
> =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=3D1; cnt}
>
> =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
>
> Jes=FAs, note that you do not need the block form of Hash.new because of
> the way + works. It is sufficient to make 0 the default element.

Oh, that's right. I'm so used to that form for defaulting to an array :-).
You say the reason is because of how + works, but is it not the reason
the fact that 0 is an immediate value
of which there's only one instance? So having the same reference to
the 0 object works, whereas having the same
reference to an array (for example) wouldn't achieve the desired effect?

Thanks,

Jesus.

Robert Klemme

3/25/2008 12:49:00 PM

0

2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> On Tue, Mar 25, 2008 at 11:23 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
> > 2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> > > On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> =
wrote:
> > > > a have an array array=3D["a","b","c","a","b"]
> > > > how can i find the numbers of "a", "b","c" items in the array?
>
> > > irb(main):006:0> a =3D %w{a b a c b a c c}
> > > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> > > irb(main):007:0> h =3D Hash.new {|h,k| h[k] =3D 0}
> > > =3D> {}
> > > irb(main):008:0> a.each {|x| h[x] +=3D 1}
> > > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> > > irb(main):009:0> h
> > > =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
> >
> > Here's another
> >
> > irb(main):001:0> a =3D %w{a b a c b a c c}
> >
> > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> > irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=3D1; cnt}
> >
> > =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
> >
> > Jes=FAs, note that you do not need the block form of Hash.new because=
of
> > the way + works. It is sufficient to make 0 the default element.
>
>
> Oh, that's right. I'm so used to that form for defaulting to an array :-)=

Jesús Gabriel y Galán

3/25/2008 2:46:00 PM

0

On Tue, Mar 25, 2008 at 1:49 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> > On Tue, Mar 25, 2008 at 11:23 AM, Robert Klemme
> > <shortcutter@googlemail.com> wrote:

> No, this has nothing to do with immediate values. The reason why this
> works is that an operation is used that creates a new instance
> (Fixnum#+ in this case) and uses assignment to place a new value on
> every iteration. This also works with arrays and other non immediate
> and even mutable instances:
>
> irb(main):001:0> collector =3D Hash.new []
> =3D> {}
> irb(main):002:0> 10.times {|i| collector[i%3] +=3D [i]}
> =3D> 10

Ah, ok, now I see what you mean. I didn't consider this case, because
you are creating intermediate arrays every iteration. In my head I was
equating the +=3D for Fixnums with << for Arrays in this idiom, if you see
what I mean.

Thanks,

Jesus.

Robert Klemme

3/25/2008 2:52:00 PM

0

2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> On Tue, Mar 25, 2008 at 1:49 PM, Robert Klemme
>
> <shortcutter@googlemail.com> wrote:
> > 2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
> > > On Tue, Mar 25, 2008 at 11:23 AM, Robert Klemme
> > > <shortcutter@googlemail.com> wrote:
>
>
> > No, this has nothing to do with immediate values. The reason why this
> > works is that an operation is used that creates a new instance
> > (Fixnum#+ in this case) and uses assignment to place a new value on
> > every iteration. This also works with arrays and other non immediate
> > and even mutable instances:
> >
> > irb(main):001:0> collector =3D Hash.new []
> > =3D> {}
> > irb(main):002:0> 10.times {|i| collector[i%3] +=3D [i]}
> > =3D> 10
>
>
> Ah, ok, now I see what you mean. I didn't consider this case, because
> you are creating intermediate arrays every iteration. In my head I was
> equating the +=3D for Fixnums with << for Arrays in this idiom, if you s=
ee
> what I mean.

That's what I would usually do. But you asked for the reason why the
default value setting was sufficient. So I demonstrated with a
mutable object and + - could have been anything else (BigDecimal#+ or
String#+ for example).

Cheers

robert

--=20
use.inject do |as, often| as.you_can - without end

Charles Calvert

3/28/2008 2:41:00 AM

0

On Tue, 25 Mar 2008 05:23:50 -0500, Robert Klemme wrote:

> 2008/3/25, Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
>> On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wrote:
>> > a have an array array=["a","b","c","a","b"]
>> > how can i find the numbers of "a", "b","c" items in the array?
>>
>>
>> Here's one way:
>>
>> irb(main):006:0> a = %w{a b a c b a c c}
>> => ["a", "b", "a", "c", "b", "a", "c", "c"]
>> irb(main):007:0> h = Hash.new {|h,k| h[k] = 0}
>> => {}
>> irb(main):008:0> a.each {|x| h[x] += 1}
>> => ["a", "b", "a", "c", "b", "a", "c", "c"]
>> irb(main):009:0> h
>> => {"a"=>3, "b"=>2, "c"=>3}
>
> Here's another

Let's see if I understand this:

> irb(main):001:0> a = %w{a b a c b a c c}
> => ["a", "b", "a", "c", "b", "a", "c", "c"]

Creates an array of strings.

> irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=1; cnt}
> => {"a"=>3, "b"=>2, "c"=>3}

Calls the method Enumerable::inject on the variable a, passing Hash.new(0)
as the initial value of memo, which is the accumulator. For each e in cnt
(which is a reference to the hash returned by "Hash.new"), it increments
the count whose key is the value of e. I would pseudocode it as this:

cnt = Hash.new(0)
foreach (e in a)
{
cnt[e] += 1
}

What does the last "cnt" do, right before the closing brace, return a
reference to the hash? I tried removing that bit:

a.inject(Hash.new(0)){|cnt,e| cnt[e]+=1}

and got the error:

TypeError: can't convert String into Integer
from (irb):2:in `[]'
from (irb):2
from (irb):2:in `inject'
from (irb):2:in `each'
from (irb):2:in `inject'
from (irb):2
from :0

I'm guessing that the use of "cnt" is controlling the interpretation of
the type returned by cnt[e], but I don't understand how.

--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celti... | Technical Writing
(703) 580-0210 | Research

Todd Benson

3/28/2008 3:12:00 AM

0

On Thu, Mar 27, 2008 at 9:44 PM, Charles Calvert <cbciv@yahoo.com> wrote:
> On Tue, 25 Mar 2008 05:23:50 -0500, Robert Klemme wrote:
>
> > 2008/3/25, Jes=FAs Gabriel y Gal=E1n <jgabrielygalan@gmail.com>:
>
> >> On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wr=
ote:
>
> >> > a have an array array=3D["a","b","c","a","b"]
> >> > how can i find the numbers of "a", "b","c" items in the array?
> >>
> >>
> >> Here's one way:
>
> >>
> >> irb(main):006:0> a =3D %w{a b a c b a c c}
> >> =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> >> irb(main):007:0> h =3D Hash.new {|h,k| h[k] =3D 0}
> >> =3D> {}
> >> irb(main):008:0> a.each {|x| h[x] +=3D 1}
> >> =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
> >> irb(main):009:0> h
> >> =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
> >
> > Here's another
>
> Let's see if I understand this:
>
>
> > irb(main):001:0> a =3D %w{a b a c b a c c}
> > =3D> ["a", "b", "a", "c", "b", "a", "c", "c"]
>
> Creates an array of strings.
>
>
> > irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=3D1; cnt}
> > =3D> {"a"=3D>3, "b"=3D>2, "c"=3D>3}
>
> Calls the method Enumerable::inject on the variable a, passing Hash.new(=
0)
> as the initial value of memo, which is the accumulator. For each e in c=
nt
> (which is a reference to the hash returned by "Hash.new"), it increments
> the count whose key is the value of e. I would pseudocode it as this:
>
> cnt =3D Hash.new(0)
> foreach (e in a)
> {
> cnt[e] +=3D 1
> }
>
> What does the last "cnt" do, right before the closing brace, return a
> reference to the hash? I tried removing that bit:
>
>
> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=3D1}
>
> and got the error:
>
> TypeError: can't convert String into Integer
> from (irb):2:in `[]'
> from (irb):2
> from (irb):2:in `inject'
> from (irb):2:in `each'
> from (irb):2:in `inject'
> from (irb):2
> from :0
>
> I'm guessing that the use of "cnt" is controlling the interpretation of
> the type returned by cnt[e], but I don't understand how.

You need to "inject" a Hash back into the block on each iteration,
which is the result of the block, which is the result of the last
statement inside the block.

If you leave that out...

h =3D a.inject(Hash.new(0)) {|k,v| k[v] +=3D 1}

...the next injection will be the result of the block, which will be
1, so on the next go around, you would be trying to...

1["a"] +=3D 1

...and thus the string error.

hth,
Todd

Robert Klemme

3/28/2008 4:10:00 PM

0

On 28.03.2008 03:41, Charles Calvert wrote:
> On Tue, 25 Mar 2008 05:23:50 -0500, Robert Klemme wrote:
>
>> 2008/3/25, Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
>>> On Tue, Mar 25, 2008 at 10:09 AM, Bu Mihai <mihai.bulhac@yahoo.com> wrote:
>>> > a have an array array=["a","b","c","a","b"]
>>> > how can i find the numbers of "a", "b","c" items in the array?
>>>
>>>
>>> Here's one way:
>>>
>>> irb(main):006:0> a = %w{a b a c b a c c}
>>> => ["a", "b", "a", "c", "b", "a", "c", "c"]
>>> irb(main):007:0> h = Hash.new {|h,k| h[k] = 0}
>>> => {}
>>> irb(main):008:0> a.each {|x| h[x] += 1}
>>> => ["a", "b", "a", "c", "b", "a", "c", "c"]
>>> irb(main):009:0> h
>>> => {"a"=>3, "b"=>2, "c"=>3}
>> Here's another
>
> Let's see if I understand this:
>
>> irb(main):001:0> a = %w{a b a c b a c c}
>> => ["a", "b", "a", "c", "b", "a", "c", "c"]
>
> Creates an array of strings.
>
>> irb(main):002:0> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=1; cnt}
>> => {"a"=>3, "b"=>2, "c"=>3}
>
> Calls the method Enumerable::inject on the variable a, passing Hash.new(0)
> as the initial value of memo, which is the accumulator. For each e in cnt
> (which is a reference to the hash returned by "Hash.new"), it increments
> the count whose key is the value of e.

Probably just a spelling error: it should read "for each e in a ...".
Your pseudo code has it actually correct.

> I would pseudocode it as this:
>
> cnt = Hash.new(0)
> foreach (e in a)
> {
> cnt[e] += 1
> }

Btw, it's not far from pseudo code to Ruby code:

irb(main):003:0> cnt = Hash.new 0
=> {}
irb(main):004:0> for e in a do
irb(main):005:1* cnt[e] += 1
irb(main):006:1> end
=> ["a", "b", "a", "c", "b", "a", "c", "c"]
irb(main):007:0> cnt
=> {"a"=>3, "b"=>2, "c"=>3}
irb(main):008:0>

:-)

> What does the last "cnt" do, right before the closing brace, return a
> reference to the hash? I tried removing that bit:
>
> a.inject(Hash.new(0)){|cnt,e| cnt[e]+=1}
>
> and got the error:
>
> TypeError: can't convert String into Integer
> from (irb):2:in `[]'
> from (irb):2
> from (irb):2:in `inject'
> from (irb):2:in `each'
> from (irb):2:in `inject'
> from (irb):2
> from :0
>
> I'm guessing that the use of "cnt" is controlling the interpretation of
> the type returned by cnt[e], but I don't understand how.

As Todd said, the return value of the block is the next iteration's
accumulator:

irb(main):009:0> (1..5).inject(0) {|*a| p a;a.first+10}
[0, 1]
[10, 2]
[20, 3]
[30, 4]
[40, 5]
=> 50

This allows elegant code, as in

irb(main):010:0> (1..5).inject(0){|s,x| s+x}
=> 15

which would not work if s was the same instance for every iteration.

Kind regards

robert



Tim McNamara

2/19/2014 4:04:00 AM

0

On Tue, 18 Feb 2014 13:08:23 -0800 (PST), van <sgcim@hotmail.com> wrote:
> That leaves us with the assertion of the sax player I mentioned: Was
> Trane's music of the last year of his life a result of his mind
> becoming unhinged by constant LSD use?

Well, biologically problems from "constant LSD use" is not very unlikely
because LSD does not produce dependence, according to the literature I
have seen. It's the nature of the chemical in the brain- basically
tolerance is built quickly and the drug loses effect with too many doses
too close together, which discourages dependence. Several of the
hallucinogenics have this in common with LSD.

But lasting psychiatric problems from hallucinogenic (LSD and other
things, as well as marijuana) use are referenced in addiction and
psychological literature. I don't know how well that has been thoroughly
and objectively documented; far too much of the published literature on
mood- and mind-altering drugs is tainted by a pro- or anti-drug agenda
which biases the data collection and/or analysis. It is possible that in
susceptible individuals even a single dose or a few doses could cause
lasting mental problems, and that in other individuals extensive use
might cause no lasting mental problems.

Unfortunately sober objective thought on this topic tends to be lacking,
replaced with hysteria and handwaving. "Turn on, tune in nd drop out"
scared the hell out of a lot of people and that is still reverberating in
a culture prone to puritannical thinking to begin with.

Trane was three years older than my parents. I can't imagine them doing
heroin or LSD. But there is at least unconfirmed speculation that Trane
did use LSD, looking around the interwebs. Lewis Porter apparently
believed that Trane did, along with having had problems with heroin and
alcohol at earlier times in his life. Did it have an adverse effect?
Beats me. Trane seemed always to be pushing and groping for something
new and maybe that search all by itself resulted in the music he was
playing at the end of his life. I don't particularly enjoy or understand
most of that stuff, although neither can I dismiss it as crazy or bad
music. I think maybe he had surrounded himself with people who would not
or could not check him on what he was playing, whereas during his days
with Miles- for example- he was given some direction and steering.