Assuming eventually your input will be a file with multiple modules, you could parse it into a more complex data structure (array of module data, where each module is represented as a hash):

use strict; use warnings; use Data::Dumper; my @modules; while(<DATA>){ push @modules, { name => $1 } if /^module (\w+) \(.*\)/; $modules[ -1 ]->{ input } = [ split /,/, $1 ] if /^input (.*);/ +; $modules[ -1 ]->{ output } = [ split /,/, $1 ] if /^output (.*); +/; $modules[ -1 ]->{ wire } = [ split /,/, $1 ] if /^wire (.*);/; next if /^endmodule/; if( /^(nand|nor|other)/ ) { push @{ $modules[ -1 ]->{ gates } }, {}; @{ $modules[ -1 ]->{ gates }[-1] }{ qw( type name output input +_A input_B) } = split /[\s\(\),;]+/; } } print Dumper \@modules; __DATA__ module circuit_17 (N1,N2,N3,N6,N7,N22,N23); input N1,N2,N3,N6,N7; output N22,N23; wire N10,N11,N16,N19; nand nand2_1 (N10,N1,N3); nand nand2_2 (N11,N3,N6); nand nand2_3 (N16,N11,N2); nand nand2_4 (N19,N11,N7); nand nand2_5 (N22,N10,N16); nand nand2_6 (N23,N16,N19); endmodule

resulting in

$VAR1 = [ { 'input' => [ 'N1', 'N2', 'N3', 'N6', 'N7' ], 'gates' => [ { 'input_A' => 'N1', 'name' => 'nand2_1', 'type' => 'nand', 'input_B' => 'N3', 'output' => 'N10' }, { 'input_A' => 'N3', 'name' => 'nand2_2', 'type' => 'nand', 'input_B' => 'N6', 'output' => 'N11' }, { 'input_A' => 'N11', 'name' => 'nand2_3', 'type' => 'nand', 'input_B' => 'N2', 'output' => 'N16' }, { 'input_A' => 'N11', 'name' => 'nand2_4', 'type' => 'nand', 'input_B' => 'N7', 'output' => 'N19' }, { 'input_A' => 'N10', 'name' => 'nand2_5', 'type' => 'nand', 'input_B' => 'N16', 'output' => 'N22' }, { 'input_A' => 'N16', 'name' => 'nand2_6', 'type' => 'nand', 'input_B' => 'N19', 'output' => 'N23' } ], 'name' => 'circuit_17', 'wire' => [ 'N10', 'N11', 'N16', 'N19' ], 'output' => [ 'N22', 'N23' ] } ];

In reply to Re: Store data into array by looping? by hdb
in thread Store data into array by looping? by ameezys

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.