Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating element string parsing and iteration

Status
Not open for further replies.

funkfactorus

Programmer
Nov 14, 2007
2
US
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!
 
A hash of arrays or an array of hashes is the way to go. I would think a hash of arrays would be good since the number of dims is constant per section. Something like:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$data[/blue] = [red]"[/red][purple]value1,value2,value3,value4,value5,value6,value7,value8,value9[/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]@sections[/blue] = [red]qw([/red][purple]A B C[/purple][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@data[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]'[/red][purple],[/purple][red]'[/red],[blue]$data[/blue][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]%section_data[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$sections[/blue][red][[/red][blue]$_[/blue][red]][/red] => [red][[/red] [blue]$data[/blue][red][[/red][blue]$_[/blue][red]][/red],[blue]$data[/blue][red][[/red][blue]$_[/blue]+[fuchsia]1[/fuchsia][red]][/red],[blue]$data[/blue][red][[/red][blue]$_[/blue]+[fuchsia]2[/fuchsia][red]][/red] [red]][/red] [red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][blue]$#sections[/blue][red])[/red][red];[/red]

each hash key is an array of the X Y Z positions. To print the values for section 'A':

Code:
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$section_data[/blue][red]{[/red][purple]A[/purple][red]}[/red]->[red][[/red][fuchsia]0[/fuchsia][red]][/red][red];[/red][gray][i]#X[/i][/gray]
[black][b]print[/b][/black] [blue]$section_data[/blue][red]{[/red][purple]A[/purple][red]}[/red]->[red][[/red][fuchsia]1[/fuchsia][red]][/red][red];[/red][gray][i]#Y[/i][/gray]
[black][b]print[/b][/black] [blue]$section_data[/blue][red]{[/red][purple]A[/purple][red]}[/red]->[red][[/red][fuchsia]2[/fuchsia][red]][/red][red];[/red][gray][i]#Z[/i][/gray]

[gray][i]#print all the values:[/i][/gray]

[black][b]print[/b][/black] [blue]@[/blue][red]{[/red] [blue]$section_data[/blue][red]{[/red][purple]A[/purple][red]}[/red] [red]}[/red][red];[/red]




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
actually, this line:

Code:
my %section_data = map {$sections[$_] => [ $data[$_],$data[$_+1],$data[$_+2] ] } (0..$#sections);


should be:

Code:
my %section_data = map {$sections[$_] => [ $data[$_*3],$data[$_*3+1],$data[$_*3+2] ] } (0..$#sections);

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top