Repeating element string parsing and iteration

T

throwaway43054

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!
 
J

John W. Krahn

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
 
M

Martijn Lievaart

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
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top