Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 26th, 2012, 04:48 AM
perl_zch perl_zch is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 perl_zch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 3 sec
Reputation Power: 0
Writing formula in perl..

Hi,

I am new to perl. I am trying to parse my xml file to my script and generating an include or define file... My xml (test.xml) looks like:

Code:

<?xml version="1.0" encoding="UTF-8"?> 
<DEFINES> 
<TEST NAME="CHAIN_UNITS" VALUE="18"></TEST> 
<TEST NAME="CHIP_UNITS" VALUE="33"></TEST> 
<TEST NAME="MUX_WIDTH" VALUE="10"></TEST> 
<TEST NAME="REGISTER_LENGTH" VALUE="9"></TEST> 
<INSTANCES> 
</INSTANCES> 
<FUB></FUB> 
<POWER_DOMAIN></POWER_DOMAIN> 
<SECURITY></SECURITY> 
</DEFINES> 


My perl script is as:

Code:
#perl script 
use strict; # Strict formatting 
use warnings; 
use Data::Dumper; 
use XML::XPath; 
use XML::XPath::XMLParser; 
use Getopt::Long; 
use Term::ANSIColor; 

print "Hello world\n"; 

my $out=""; 
my $length=""; 
#create an object to parse the file 
my $xp = XML::XPath->new(filename => 'test.xml'); 
my $nodeset =$xp->findnodes('/DEFINES/TEST'); 
$out.="//\n"; 
$out.="//\n"; 

foreach my $node( $nodeset->get_nodelist) 
{ 
my $namenode=$node->getAttributeNode("NAME"); 
my $name=$namenode->getValue; 
my $namelow=lc$name; 

my $valuenode=$node->getAttributeNode("VALUE"); 
my $value=$valuenode->getValue; 
my $valuelow=lc$value; 

$out.="`define $name $value\n"; 
} 

foreach my $node( $nodeset->get_nodelist) 
{ 
my $namenode=$node->getAttributeNode("NAME"); 
my $name=$namenode->getValue; 
my $namelow=lc$name; 

my $valuenode=$node->getAttributeNode("VALUE"); 
my $value=$valuenode->getValue; 
my $valuelow=lc$value; 


$sdi_length="`define BOUNDARY_LENGTH "; 
} 

my $filename="./network/defines.sv"; 
open (FILE, "> $filename") or die ("Error: could not open file $filename for writing\n"); 
print FILE $out; 
print FILE $length; 
close FILE; 


The formulae for calculating BOUNDARY_LENGTH is:

BOUNDARY_LENGTH = CHAIN_UNITS*MUX_WIDTH + CHIP_UNITS*MUX_WIDTH

My question here is that, How can I plug this formula into my script so that it gives me output in the file define.sv as "`define BOUNDARY_LENGTH 510". There is no error in the above script. Kindly help me in using this formula in my script.

Thanks.

Reply With Quote
  #2  
Old September 26th, 2012, 11:40 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
How about this (for a hint)
Code:
#!/usr/bin/perl -w
#perl script 
use strict; # Strict formatting 
use warnings; 
use Data::Dumper; 
use XML::XPath; 
use XML::XPath::XMLParser; 
use Getopt::Long; 
use Term::ANSIColor; 

my $xp = XML::XPath->new(filename => 'foo.xml'); 
my $nodeset =$xp->findnodes('/DEFINES/TEST'); 
my %symtable;

foreach my $node( $nodeset->get_nodelist) 
{ 
    my $namenode=$node->getAttributeNode("NAME"); 
    my $name=$namenode->getValue; 
    my $namelow=lc$name; 

    my $valuenode=$node->getAttributeNode("VALUE"); 
    my $value=$valuenode->getValue; 
    my $valuelow=lc$value; 

    $symtable{$namelow} = $valuelow;
} 

# BOUNDARY_LENGTH = CHAIN_UNITS*MUX_WIDTH + CHIP_UNITS*MUX_WIDTH
# <TEST NAME="CHAIN_UNITS" VALUE="18"></TEST> 
# <TEST NAME="CHIP_UNITS" VALUE="33"></TEST> 
# <TEST NAME="MUX_WIDTH" VALUE="10"></TEST> 
# <TEST NAME="REGISTER_LENGTH" VALUE="9"></TEST> 
my $answer = $symtable{"chain_units"} * $symtable{"mux_width"} +
        $symtable{"chip_units"} * $symtable{"mux_width"};
print "Answer=$answer\n";


$ perl foo.pl
Answer=510


If there is any chance at all that the input XML can be malformed, then you should guard the evaluation with a test for things like
Code:
if ( defined $symtable{"chain_units"} && defined $symtable{"mux_width"} )
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old September 27th, 2012, 06:23 AM
perl_zch perl_zch is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 perl_zch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 3 sec
Reputation Power: 0
got it.. Thanks. It worked for me.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Writing formula in perl..

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap