Repeating element string parsing and iteration

Discussion in 'Perl Misc' started by throwaway43054@gmail.com, Nov 15, 2007.

  1. Guest

    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!
     
    , Nov 15, 2007
    #1
    1. Advertising

  2. "" 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
     
    John W. Krahn, Nov 15, 2007
    #2
    1. Advertising

  3. On Wed, 14 Nov 2007 21:47:12 -0800, 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
     
    Martijn Lievaart, Nov 15, 2007
    #3
  4. Guest

    Thank you both!
     
    , Nov 15, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Lipp
    Replies:
    1
    Views:
    414
    Patrick TJ McPhee
    Jan 30, 2004
  2. Wolfgang Lipp
    Replies:
    0
    Views:
    493
    Wolfgang Lipp
    Jan 28, 2004
  3. Replies:
    2
    Views:
    685
  4. Javier
    Replies:
    0
    Views:
    411
    Javier
    Apr 18, 2008
  5. Rudi
    Replies:
    5
    Views:
    5,224
Loading...

Share This Page