use warnings; use strict; use Data::Dumper; my @names = qw(opens first last inc noise min max inc); my $patterns = "1 10 100 10 0 0 0 0#2 10 100 10 1 3 5 2#3 20 100 10 0 +3 5 2"; my @data; for my $set (split /#/, $patterns) { my @vals = split /\s+/, $set; my %hash; for my $i (0 .. $#vals) { $hash{$names[$i]} = $vals[$i]; } push @data, \%hash; } print Dumper(\@data); __END__ $VAR1 = [ { 'first' => '10', 'opens' => '1', 'min' => '0', 'inc' => '0', 'max' => '0', 'last' => '100', 'noise' => '0' }, { 'first' => '10', 'opens' => '2', 'min' => '3', 'inc' => '2', 'max' => '5', 'last' => '100', 'noise' => '1' }, { 'first' => '20', 'opens' => '3', 'min' => '3', 'inc' => '2', 'max' => '5', 'last' => '100', 'noise' => '0' } ];

Update: I just saw jdporter's comment in the CB about "you have two elements in the hash named 'inc'. You can only have one."


In reply to Re: Help creating a data structure out of a delimited string by toolic
in thread Help creating a data structure out of a delimited string by Conal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.