Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found