Perl array manipulation questions

B

Bryan

Hi,

Two questions, which may be related...

1. If I have an array:
@a = (1, 2, 3, 4);

And I want to process each element in the array with some algorithm,
lets say multiply each element by 2, is there a way to do this without
creating a temp array, and -without- looping?

Something along the lines of @a * 2, and @a would then be (2, 4, 6, 8)?

2. If I have two arrays:
@a = (1, 2, 3, 4);
@b = (2, 4, 6, 8);

Is there a way to create a 3rd array of the same size such that @c = @a
+ @b, and after the operation @c = (3, 6, 9, 12). Again, without using
a for or a foreach loop??

Thanks,
Bryan
 
B

Ben Morrow

Quoth Bryan said:
1. If I have an array:
@a = (1, 2, 3, 4);

And I want to process each element in the array with some algorithm,
lets say multiply each element by 2, is there a way to do this without
creating a temp array, and -without- looping?

Well... you can't do it without a loop, but map provides one implicitly:

@a = map { $_ * 2 } @a;
2. If I have two arrays:
@a = (1, 2, 3, 4);
@b = (2, 4, 6, 8);

Is there a way to create a 3rd array of the same size such that @c = @a
+ @b, and after the operation @c = (3, 6, 9, 12). Again, without using
a for or a foreach loop??

@c = map { $a[$_] + $b[$_] } 0..$#a;

You may also want to look at List::Util for more map-likes.

Ben
 
B

Ben Morrow

Quoth Ben Morrow said:
Well... you can't do it without a loop, but map provides one implicitly:

@a = map { $_ * 2 } @a;

In this case, of course, using a loop is actually easier:

$_ *= 2 for @a;

Ben
 
J

Jim Cochrane

In this case, of course, using a loop is actually easier:

$_ *= 2 for @a;

Ben

And if you want to get really fancy, you can define a class/module and
redefine the '*' operator. (Whether this is wise design is another issue.)
 
U

Uri Guttman

BM> @c = map { $a[$_] + $b[$_] } 0..$#a;

if you can afford to destroy one of those arrays, it can be done without
indexing (which is slowish and i just prefer to skip if not needed).

@c = map { shift @a + $_ } @b ;

uri
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

Hi,

Two questions, which may be related...

1. If I have an array:
@a = (1, 2, 3, 4);

And I want to process each element in the array with some algorithm,
lets say multiply each element by 2, is there a way to do this without
creating a temp array, and -without- looping?

You can use the map function as follows :

@a2 = map { $_ * 2 } @a;

Something along the lines of @a * 2, and @a would then be (2, 4, 6, 8)?

2. If I have two arrays:
@a = (1, 2, 3, 4);
@b = (2, 4, 6, 8);

Is there a way to create a 3rd array of the same size such that @c = @a
+ @b, and after the operation @c = (3, 6, 9, 12). Again, without using
a for or a foreach loop??

@sum = map { $a[$_] + $b[$_] } ( 0 .. $#a );
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top