473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

sum up array

Hi, I am trying to sum up all the second element in an array (@hello). I tried as below but cant work. Suppose i could get 12 ( 3+ 4 + 5)
please guide. thx

Expand|Select|Wrap|Line Numbers
  1. my @hello =();
  2. push (@hello, join(",", 'A', 3));
  3. push (@hello, join(",", 'B', 4));
  4. push (@hello, join(",", 'C', 5));
  5.  
  6. $helo=(@hello);
  7.  
  8. for($y=0;$y<$helo;$y++) {
  9. push(@out, pop(@hello[$y]));
  10. }
  11. my $total = 0; 
  12. $total_in_array += $_ foreach @out ;
Apr 11 '08 #1
5 1836
KevinADC
4,059 Expert 2GB
Your @hello array will have three values:

,A3
,B4
,C5


If you wanted to add up the number part of each element of the array you first have to remove the no-digit characters. But I am not sure what you are really trying to do. The use of the join() function appears to be completely wrong since it joins the three things together to make one string, for example ,A3. The use of push and pop is not necessary at all.
Apr 11 '08 #2
use Data::Dumper;

Expand|Select|Wrap|Line Numbers
  1. my @hello =();  # this is just to get a sample data; what i want is to sum up the 2nd element in the array.
  2. push (@hello, join(",", 'A', 3));
  3. push (@hello, join(",", 'B', 4));
  4. push (@hello, join(",", 'C', 5));
  5.  
  6. print Dumper(@hello);
  7. exit;
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = 'A,3';
  2. $VAR2 = 'B,4';
  3. $VAR3 = 'C,5';
  4.  
from @hello, I wish to get the 2nd element of each array and then sum them up.
Means, i would like to add 3+4+5 = 12.
Apr 11 '08 #3
KevinADC
4,059 Expert 2GB
Sorry, somehow I misread your code and thought the data would be different, of course it will be as Data dumper shows:

$VAR1 = 'A,3';
$VAR2 = 'B,4';
$VAR3 = 'C,5';

in which case you could do this instead of all that unecessary stuff you were doing:

Expand|Select|Wrap|Line Numbers
  1. my @hello =();
  2. push (@hello, join(",", 'A', 3));
  3. push (@hello, join(",", 'B', 4));
  4. push (@hello, join(",", 'C', 5));
  5. my $total = 0;
  6. for (@hello) {
  7.    $total += (split',')[1];
  8. }
  9. print $total;
all though I am not sure why you are making this so complicated:

Expand|Select|Wrap|Line Numbers
  1. push (@hello, join(",", 'A', 3));
  2. push (@hello, join(",", 'B', 4));
  3. push (@hello, join(",", 'C', 5));
just write it as:

Expand|Select|Wrap|Line Numbers
  1. my @hello = qw (A,3 B,4 C,5);
But whatever turns you on mate. It's your code. :)
Apr 11 '08 #4
KevinADC
4,059 Expert 2GB
You have some recent threads you posted and never replied to them. If you continue to do that I will no longer help you. That is my personal policy and not the policy of this website.
Apr 11 '08 #5
Noted.
Sorry.. i will take note next time.
Thank you.
Apr 11 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.