Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Store data into array by looping?

by hdb (Monsignor)
on Apr 10, 2019 at 09:51 UTC ( #1232396=note: print w/replies, xml ) Need Help??


in reply to Store data into array by looping?

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' ] } ];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1232396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-07-12 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?