![]() |
Repeating element string parsing and iteration
Hi all! Noob to perl here...
I have widget with X sections, each having Y dimensions. The amount of sections is dynamic, but amount of dimensions per section is static (let's say it is always 3 dimensions per section). I have a scalar which is a comma-delimited string of numeric values representing values for all the combinations of section and dimensions, such as: $data = "value1,value2,value4,value5,value6,value7, ..." So the first three values belong to first section, values 4-6 belong to second section and so on. I also have an array with names of the sections. @sections= ("A, B, C, D"); I also have names for the dimenstions: @dim_names = ("Xpos", "Ypos", "Zpos")' I want to build a hash such as: %section_data = qw/ "A-Xpos" => "value1", "A-Ypos" => "value2", "A-Zpos" => "value3", "B-Xpos" => "value4", "B-Ypos" => "value5", ... ... /; I'm not really sure how to tackle this. I was thinking of maybe doing nested loops for section names and dimension names, chipping away at @data using somehting like /\G.,?/g, but that seem a bit awkward and I would love to know a better way. Any help is much appreciated. Abstract code samples would be great! Thanks in advance! |
Re: Repeating element string parsing and iteration
"throwaway43054@gmail.com" wrote:
> > I have widget with X sections, each having Y dimensions. The amount of > sections is dynamic, but amount of dimensions per section is static > (let's say it is always 3 dimensions per section). > > I have a scalar which is a comma-delimited string of numeric values > representing values for all the combinations of section and > dimensions, such as: > $data = "value1,value2,value4,value5,value6,value7, ..." > So the first three values belong to first section, values 4-6 belong > to second section and so on. > > I also have an array with names of the sections. > @sections= ("A, B, C, D"); > > I also have names for the dimenstions: > @dim_names = ("Xpos", "Ypos", "Zpos")' > > I want to build a hash such as: > %section_data = qw/ > "A-Xpos" => "value1", > "A-Ypos" => "value2", > "A-Zpos" => "value3", > "B-Xpos" => "value4", > "B-Ypos" => "value5", > ... > ... > /; > > I'm not really sure how to tackle this. I was thinking of maybe doing > nested loops for section names and dimension names, chipping away at > @data using somehting like /\G.,?/g, but that seem a bit awkward and I > would love to know a better way. Any help is much appreciated. > Abstract code samples would be great! Thanks in advance! Something like (UNTESTED): my @fields = split /,/, $data, -1; my %section_data; for my $section ( @sections ) { for my $dim_name ( @dim_names ) { $section_data{ "$section-$dim_name" } = shift @fields; } } John -- use Perl; program fulfillment |
Re: Repeating element string parsing and iteration
On Wed, 14 Nov 2007 21:47:12 -0800, throwaway43054@gmail.com wrote:
> Hi all! Noob to perl here... > Hi, > I want to build a hash such as: > %section_data = qw/ You don't want qw her, just an opening parenthesis. > "A-Xpos" => "value1", > "A-Ypos" => "value2", > "A-Zpos" => "value3", > "B-Xpos" => "value4", > "B-Ypos" => "value5", > ... > ... > /; > > > I'm not really sure how to tackle this. I was thinking of maybe doing > nested loops for section names and dimension names, chipping away at > @data using somehting like /\G.,?/g, but that seem a bit awkward and I > would love to know a better way. Any help is much appreciated. Abstract > code samples would be great! Thanks in advance! Your direction is sound. Something like (untested): my @data = split(/,/, $data); my %section_data; for my $section = (@sections) { for my $dim = (@dim_names) { $section_data{"$section-$dim"} = shift @data; } } Or shorter: my @data = split(/,/, $data); my %section_data; for my $section = (@sections) { $section_data{"$section-$_"} = shift @data for (@dim_names); } Any shorter would be obfuscation IMO. HTH, M4 |
Re: Repeating element string parsing and iteration
Thank you both!
|
| All times are GMT. The time now is 03:51 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.