Jim,

Thanks for your feedback. I am actually trying to sum up all rlptxat elements 
that have the same cell, sect, and chan elements in the array. I hope this 
clarifies.

Thank you,

Chris 

-----Original Message-----
From: Jim Gibson [mailto:jimsgib...@gmail.com] 
Sent: Sunday, March 20, 2011 5:58 PM
To: beginners@perl.org
Subject: RE: foreach loop

At 8:31 AM -0600 3/20/11, Chris Stinemetz wrote:
>So back to the question. How can I nest this foreach loop correctly 
>to add all occurrences of the element rlptxat that have the same 
>elements cell, sect and chan in the array?
>
>$record{rlptxat} = ( length($record{rlptxat})> 1 ) ? 
>$record{rlptxat}{cell, sect, chan, dist}++ : '' ;

That doesn't compile on my system under 'use strict:' because cell, 
sect, chan, dist are "barewords", i.e. not variables or strings. They 
will look more like parameterless functions to the Perl interpreter. 
While they are allowed in some circumstances under 'strict', this is 
not one of them.

You should make them explicitly strings, either as ('cell', 'sect', 
'chan', 'dist') or as qw( cell sect chan dist) (note the absence of 
commas in the second version.

Since you are assigning a hash slice, which has multiple values, you 
should use @record{...} instead of $record{...}, which is a single, 
scalar value:

@record{rlptxat} = ( length($record{rlptxat})> 1 ) ? 
@record{rlptxat}{ qw(cell sect chan dist)}++ : '' ;

However, it is not clear what you are trying to do. The rlptxat 
element is the 45th field on the input line. Are you trying to 
replace that element with a reference to a hash containing 4 elements 
from elsewhere in the line? Or are you trying to increment the values 
in those other 4 elements.

Try writing multiple lines to do what you want. Don't try to stuff 
everything into one line until you know Perl better. If you write a 
bunch of lines to do something, people here will gladly tell you how 
you could have done it in fewer lines.

-- 
Jim Gibson
j...@gibson.org

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to