Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array +=

Reply
Thread Tools

Array +=

 
 
Simon Mullis
Guest
Posts: n/a
 
      10-12-2007
Hi All,

Question re Array method += and

>> a = []

=> []
>> a += [1, 2, 3, 4]

=> [1, 2, 3, 4]

And

>> (a ||= []) << [1, 2, 3, 4]

=> [[1, 2, 3, 4]]

But

>> ( a ||= [] ) += [ 1, 2, 3, 4 ]

SyntaxError: compile error
(irb):28: syntax error, unexpected tOP_ASGN, expecting $end
( a ||= [] ) += [ 1, 2, 3, 4 ]
^
from (irb):28

Why?

I guess it's a precedence issue. '<<' is a method in the Array class.
'+=' is not.

Is there alternate syntax I should use?

Thanks

SM

 
Reply With Quote
 
 
 
 
mortee
Guest
Posts: n/a
 
      10-12-2007
Simon Mullis wrote:
>>> (a ||= []) << [1, 2, 3, 4]

> => [[1, 2, 3, 4]]
>
> But
>
>>> ( a ||= [] ) += [ 1, 2, 3, 4 ]

> SyntaxError: compile error
> (irb):28: syntax error, unexpected tOP_ASGN, expecting $end
> ( a ||= [] ) += [ 1, 2, 3, 4 ]
> ^
> from (irb):28
>
> Why?
>
> I guess it's a precedence issue. '<<' is a method in the Array class.
> '+=' is not.


The difference is that << is a method, += is an assignment. (a ||= [])
returns an object, on which you can call a method, but you can't assign
to. You can only assign to the a variable itself, which (a ||= []) is
not. I guess.

mortee


 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      10-12-2007
Simon Mullis wrote:
> Is there alternate syntax I should use?
>


Did you you mean this?

(a ||= []).concat [ 1, 2, 3, 4 ]

concat modifies a, like << would. It's usually faster to concatenate
than to add and reassign.

--
Florian Frank


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-12-2007
2007/10/12, mortee <(E-Mail Removed)>:
> Simon Mullis wrote:
> >>> (a ||= []) << [1, 2, 3, 4]

> > => [[1, 2, 3, 4]]
> >
> > But
> >
> >>> ( a ||= [] ) += [ 1, 2, 3, 4 ]

> > SyntaxError: compile error
> > (irb):28: syntax error, unexpected tOP_ASGN, expecting $end
> > ( a ||= [] ) += [ 1, 2, 3, 4 ]
> > ^
> > from (irb):28
> >
> > Why?
> >
> > I guess it's a precedence issue. '<<' is a method in the Array class.
> > '+=' is not.


No, it's not a precedence issue. Btw, you are comparing apples and
oranges here: += and << are not equivalent (see also further below):

irb(main):001:0> a=%w{foo bar}
=> ["foo", "bar"]
irb(main):002:0> b=a.dup
=> ["foo", "bar"]
irb(main):003:0> c=a.dup
=> ["foo", "bar"]
irb(main):004:0> b << a
=> ["foo", "bar", ["foo", "bar"]]
irb(main):005:0> c += a
=> ["foo", "bar", "foo", "bar"]

<< adds the whole Array as one object while += "appends" the Array.
You rather want Array#concat.

> The difference is that << is a method, += is an assignment. (a ||= [])
> returns an object, on which you can call a method, but you can't assign
> to. You can only assign to the a variable itself, which (a ||= []) is
> not. I guess.


Exactly: the expression (a||=[]) is not an lvalue, i.e. cannot
assigned to. But you can do

( a ||= [] ).concat [ 1, 2, 3, 4 ]

Simon, please also keep in mind that += and << have different
semantics. += will create a new Array while << appends to the current
one.

Kind regards

robert

 
Reply With Quote
 
Simon Mullis
Guest
Posts: n/a
 
      10-12-2007
I love this list... I send a mail, go off to a meeting, come back and
I have explanation, discussion and solution!

Thanks to all who responded.

Cheers

SM

On 10/12/07, Robert Klemme <(E-Mail Removed)> wrote:
> 2007/10/12, mortee <(E-Mail Removed)>:
> > Simon Mullis wrote:
> > >>> (a ||= []) << [1, 2, 3, 4]
> > > => [[1, 2, 3, 4]]
> > >
> > > But
> > >
> > >>> ( a ||= [] ) += [ 1, 2, 3, 4 ]
> > > SyntaxError: compile error
> > > (irb):28: syntax error, unexpected tOP_ASGN, expecting $end
> > > ( a ||= [] ) += [ 1, 2, 3, 4 ]
> > > ^
> > > from (irb):28
> > >
> > > Why?
> > >
> > > I guess it's a precedence issue. '<<' is a method in the Array class.
> > > '+=' is not.

>
> No, it's not a precedence issue. Btw, you are comparing apples and
> oranges here: += and << are not equivalent (see also further below):
>
> irb(main):001:0> a=%w{foo bar}
> => ["foo", "bar"]
> irb(main):002:0> b=a.dup
> => ["foo", "bar"]
> irb(main):003:0> c=a.dup
> => ["foo", "bar"]
> irb(main):004:0> b << a
> => ["foo", "bar", ["foo", "bar"]]
> irb(main):005:0> c += a
> => ["foo", "bar", "foo", "bar"]
>
> << adds the whole Array as one object while += "appends" the Array.
> You rather want Array#concat.
>
> > The difference is that << is a method, += is an assignment. (a ||= [])
> > returns an object, on which you can call a method, but you can't assign
> > to. You can only assign to the a variable itself, which (a ||= []) is
> > not. I guess.

>
> Exactly: the expression (a||=[]) is not an lvalue, i.e. cannot
> assigned to. But you can do
>
> ( a ||= [] ).concat [ 1, 2, 3, 4 ]
>
> Simon, please also keep in mind that += and << have different
> semantics. += will create a new Array while << appends to the current
> one.
>
> Kind regards
>
> robert
>
>


 
Reply With Quote
 
Simon Mullis
Guest
Posts: n/a
 
      10-12-2007
Thanks for this Robert.

The difference between the two is exactly why I wanted to use += and
not <<. I want to add many arrays to one big one and not have to use
the extra step of Array#flatten at the end.

SM



On 10/12/07, Robert Klemme <(E-Mail Removed)> wrote:
> 2007/10/12, mortee <(E-Mail Removed)>:
> > Simon Mullis wrote:
> > >>> (a ||= []) << [1, 2, 3, 4]
> > > => [[1, 2, 3, 4]]
> > >
> > > But
> > >
> > >>> ( a ||= [] ) += [ 1, 2, 3, 4 ]
> > > SyntaxError: compile error
> > > (irb):28: syntax error, unexpected tOP_ASGN, expecting $end
> > > ( a ||= [] ) += [ 1, 2, 3, 4 ]
> > > ^
> > > from (irb):28
> > >
> > > Why?
> > >
> > > I guess it's a precedence issue. '<<' is a method in the Array class.
> > > '+=' is not.

>
> No, it's not a precedence issue. Btw, you are comparing apples and
> oranges here: += and << are not equivalent (see also further below):
>
> irb(main):001:0> a=%w{foo bar}
> => ["foo", "bar"]
> irb(main):002:0> b=a.dup
> => ["foo", "bar"]
> irb(main):003:0> c=a.dup
> => ["foo", "bar"]
> irb(main):004:0> b << a
> => ["foo", "bar", ["foo", "bar"]]
> irb(main):005:0> c += a
> => ["foo", "bar", "foo", "bar"]
>
> << adds the whole Array as one object while += "appends" the Array.
> You rather want Array#concat.
>
> > The difference is that << is a method, += is an assignment. (a ||= [])
> > returns an object, on which you can call a method, but you can't assign
> > to. You can only assign to the a variable itself, which (a ||= []) is
> > not. I guess.

>
> Exactly: the expression (a||=[]) is not an lvalue, i.e. cannot
> assigned to. But you can do
>
> ( a ||= [] ).concat [ 1, 2, 3, 4 ]
>
> Simon, please also keep in mind that += and << have different
> semantics. += will create a new Array while << appends to the current
> one.
>
> Kind regards
>
> robert
>
>



--
Simon Mullis
_________________
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-12-2007
2007/10/12, Simon Mullis <(E-Mail Removed)>:
> Thanks for this Robert.
>
> The difference between the two is exactly why I wanted to use += and
> not <<. I want to add many arrays to one big one and not have to use
> the extra step of Array#flatten at the end.


That's why you should be using #concat (more efficient) unless you do
not want to modify the original array. Read the solutions properly!


Cheers

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments