[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Compare Array Values?

Morton Goldberg

11/23/2006 2:03:00 AM

On Nov 22, 2006, at 8:48 PM, Jason Mayer wrote:

> I thought I was going to get to contribute, but then I got stuck. I
> installed the latest ruby, wrote a quick program that I thought
> would do it,
> and it didn't. So I expanded on the program, trying to figure out
> what was
> going on, and now it's 20 lines long(mostly puts) and I *still*
> don't get
> something (Beware - the single most basic code ever follows - it was
> originally much nicer but then I got to trying to debug)
>
> a = Array.new
> a = %w{test test2 test3}
> puts a
> aSize = a.size
> b = Array.new
> b = %w{test3 nil test2 test}
> bSize = b.size
> puts 'size of b:' + bSize.to_s
> puts b
> puts 'size of a:' + aSize.to_s
> if aSize = bSize

In Ruby = is only used for assignment. For testing use ==.

> aSorted= a.sort!
> bSorted= b.sort!
> if aSorted.eql?(bSorted)
> puts 'ssdd'
> else
> puts 'same size, different values'
> end
> else
> puts 'not the same size'
> end
>
>> ruby array.rb
> test
> test2
> test3
> size of b:4
> test3
> nil
> test2
> test
> size of a:3
> same size, different values
>
> What's wrong with if aSize = bSize? That seems the most intuitive
> way to do
> it to me... I mean, it's an integer... why is it going inside the
> loop at
> all?

Regards, Morton



2 Answers

Claudio V

11/23/2006 2:09:00 AM

0

#unsuscribe

2006/11/23, Morton Goldberg <m_goldberg@ameritech.net>:
> On Nov 22, 2006, at 8:48 PM, Jason Mayer wrote:
>
> > I thought I was going to get to contribute, but then I got stuck. I
> > installed the latest ruby, wrote a quick program that I thought
> > would do it,
> > and it didn't. So I expanded on the program, trying to figure out
> > what was
> > going on, and now it's 20 lines long(mostly puts) and I *still*
> > don't get
> > something (Beware - the single most basic code ever follows - it was
> > originally much nicer but then I got to trying to debug)
> >
> > a = Array.new
> > a = %w{test test2 test3}
> > puts a
> > aSize = a.size
> > b = Array.new
> > b = %w{test3 nil test2 test}
> > bSize = b.size
> > puts 'size of b:' + bSize.to_s
> > puts b
> > puts 'size of a:' + aSize.to_s
> > if aSize = bSize
>
> In Ruby = is only used for assignment. For testing use ==.
>
> > aSorted= a.sort!
> > bSorted= b.sort!
> > if aSorted.eql?(bSorted)
> > puts 'ssdd'
> > else
> > puts 'same size, different values'
> > end
> > else
> > puts 'not the same size'
> > end
> >
> >> ruby array.rb
> > test
> > test2
> > test3
> > size of b:4
> > test3
> > nil
> > test2
> > test
> > size of a:3
> > same size, different values
> >
> > What's wrong with if aSize = bSize? That seems the most intuitive
> > way to do
> > it to me... I mean, it's an integer... why is it going inside the
> > loop at
> > all?
>
> Regards, Morton
>
>
>
>

Brad Tilley

11/23/2006 4:10:00 AM

0

Quoting Jason Mayer <slamboy@gmail.com>:

> Eek. There goes my street cred.

Don't feel too bad. That's a very common mistake. Heck, even hackers try to slip
it past experienced programmers occasionally... remember the uid=0 hack
discovered in the linux kernel a few years back?