Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

help to write perl code

by achs (Initiate)
on Aug 31, 2015 at 09:36 UTC ( [id://1140514]=perlquestion: print w/replies, xml ) Need Help??

achs has asked for the wisdom of the Perl Monks concerning the following question:

I need help to write a perl script for printing out the initial values of the bit fields of a register as a 8 bit value. The information available in the file is like
<reg> ... <bit id= 7> <name>A</name> <init_val>1</init_val> </bit> <bit id= 6> <name>B</name> <init_val>0<init_val> </bit> ...likewise bits upto 0 </reg>
Likewise the file contains of 100 regs. I want to print out the 'init_val' of all the bit fields of the registers. I have tried writing the script
while($line=<IN>) { .... #reg bit field if($line=~/\<bit\sid\=\"(.*)\"\>/) { $bit_name=$1; ... print "Line no. where the bit field has been found : $.\n\n"; #As 'init_val' occurs on the second line after 'bit id' $line_count=$.+2; print "The incremented value is $line_count\n\n "; #reading the initial_value print "####################\n\n\n"; while($sub_line=<IN>) { print "**********************Entered the second loop****** +************\n\n\n"; print "The line no is $.\n\n"; if($.==$line_count) { if($sub_line=~/\<INIT\_VAL\>(.*)\<\/INIT_VAL\>/) { $bits{"$bit_name"}=$1; print "...................The bit field initial value +is $1......... \n\n" } if($sub_line=~/\<INIT\_VAL\/\>/) { $bits{"$bit_name"}=0; print "The bit field initial value is 0\n\n\n " } } print "End of the Loop\n\n"; %bits=qw(0 0); } print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n"; $.=$line_count; print "THE LINE COUNT IS $.\n\n "; } ... }
But the above code is not working properly. After the first bit field,it is getting exited. It is not going to other bit fields of the reg as well as not other registers in the file

Replies are listed 'Best First'.
Re: HELP TO WRITE THE PERL CODE
by choroba (Cardinal) on Aug 31, 2015 at 10:03 UTC
    Hi achs, welcome to the Monastery!

    This site isn't a code writing service, its purpose is learning. What have you tried? What problems have you had?

    Haven't you simplified the format of the input file? <bit 7> can't occur in an XML file, as only attributes are allowed after an element name, and an attribute's name can't start with a digit, and must be followed by a = and the value.

    Update: Also, sentences in uppercase are usually interpreted as shouting. Don't shout without a good reason.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: HELP TO WRITE THE PERL CODE
by QuillMeantTen (Friar) on Aug 31, 2015 at 16:20 UTC

    This or if you prefer, linked as this : YAML module should greatly help you, I have started giving it a good look for configuration file parsing purposes and it might very well be the perfect tool for your task.

    also, as said before you should first think about writing a script to spot and correct the xml syntax errors in your input file
    Cheers!

    Update, another link better formatted this time that will complete your toolchain : xml2yaml

Re: help to write perl code
by poj (Abbot) on Sep 01, 2015 at 08:24 UTC

    Your program gets stuck in the $sub_line=<IN> loop even after finding the INIT_VAL tag, add  last; to break out.

    To see this add $line_count to this line  print "The line no is $. looking for $line_count\n";

    poj
Re: HELP TO WRITE THE PERL CODE
by Anonymous Monk on Sep 01, 2015 at 02:39 UTC
    Is the format supposed to be xml?

      perlintro, xpather.pl , Modern Perl, ...

      #!/usr/bin/perl -- ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use XML::LibXML 1.70; ## for load_html/load_xml/location my( $xml ) = q{<reg><bit><init_val>0</init_val></bit><bit><init_val>0< +/init_val></bit><bit><init_val>1</init_val></bit><bit><init_val>0</in +it_val></bit><bit><init_val>1</init_val></bit><bit><init_val>1</init_ +val></bit><bit><init_val>0</init_val></bit><bit><init_val>0</init_val +></bit><bit><init_val>0</init_val></bit><bit><init_val>1</init_val></ +bit><bit><init_val>0</init_val></bit><bit><init_val>0</init_val></bit +><bit><init_val>1</init_val></bit><bit><init_val>1</init_val></bit><b +it><init_val>0</init_val></bit><bit><init_val>0</init_val></bit></reg +>}; RunWithIt( $xml ); exit( 0 ); sub RunWithIt { my $dom = XML::LibXML->new( qw/ recover 2 / )->load_xml( string => + shift ); for my $reg ( $dom->findnodes( q{ //reg } ) ) { my $bitstr = FunWithIt( $reg ); my $bytes = BitsToBytes( $bitstr ); print "$bitstr The answer is $bytes\n"; } } ## end sub RunWithIt sub BitsToBytes { return pack 'b*', shift; } ## end sub BitsToBytes sub FunWithIt { my( $reg ) = @_; my $str = ''; for my $bit ( $reg->findnodes( q{ //bit } ) ) { $str .= $bit->findvalue( './/init_val' ); } return $str; } ## end sub FunWithIt
        Thanks very much for the reply. I see that you have given in a variable. My input xml file is huge which I cannot copy and paste in the script. Is there a way in which I could pass the input file into the code given above and then do the processing.
Re: help to write perl code
by Anonymous Monk on Sep 02, 2015 at 00:29 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1140514]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found