Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Repeating element string parsing and iteration

Reply
Thread Tools

Repeating element string parsing and iteration

 
 
throwaway43054@gmail.com
Guest
Posts: n/a
 
      11-15-2007
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!
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-15-2007
"(E-Mail Removed)" 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
 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      11-15-2007
On Wed, 14 Nov 2007 21:47:12 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
throwaway43054@gmail.com
Guest
Posts: n/a
 
      11-15-2007
Thank you both!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
for_each iteration in a std::list adds a new element and nextiteration crashes Javier C++ 0 04-18-2008 08:47 AM
Basic dynamic repeating form element in ASP.NET2.0? mj.redfox.mj@gmail.com ASP .Net 2 04-27-2007 08:51 AM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments