Home > Community > Forums > Custom IC SKILL > How to parse the contents of a file?

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 How to parse the contents of a file? 

Last post Mon, Dec 17 2012 8:08 AM by ztzg. 15 replies.
Started by Messi 15 Nov 2010 11:04 PM. Topic has 15 replies and 4622 views
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts:
  • Mon, Nov 15 2010 11:04 PM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    How to parse the contents of a file? Reply

    Hi everyone,

        If for example i have some file say rules file with contents of file being : <ruleno.> <rule name> <value>.

       How to parse the contents of this file and store in  table format?

      Can i get code for this?

    Thanks,

      Messi

    • Post Points: 35
  • Mon, Nov 15 2010 11:09 PM

    Re: How to parse the contents of a file? Reply
    Hi Messi,

    Can you give an example of the file contents? In particular knowing the delimiters would help. Your description was not that precise...

    Regards,

    Andrew
    • Post Points: 20
  • Mon, Nov 15 2010 11:19 PM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Thanks for the quick reply...

    I will set an example: infile("/messi/Exp/rulecheck.il") looks like

       R1A  POLY WIDTH 0.5

          R1B ACTIVE WIDTH 0.4

       R2A POLY SEP    0.25

            R2B ACTIVE SEP    0.75

       R3 POLY OVERLAP 0.6

       ......................................

      ........................................

    ie..<ruleno.> <space> <layername> <space> <type> <space> <value>

    So basically i want to read this file and use parseString and store contents in table.I understand that i have to use infile command and then parseString with "space".but i am not able to put all these into a code..also i would like to make it into a table with all the values read from the file. The table contents should be like:

             <ruleno.>     <layer used(eg:POLY)>  <type(eg:WIDTH)>  <value>

    So, basically if i feed my rules filename in the procedure i should be able to see its contents in an tabulated form.

    Thanks

     Messi

    • Post Points: 20
  • Tue, Nov 16 2010 2:58 AM

    • LeJonT
    • Top 500 Contributor
    • Joined on Thu, Apr 1 2010
    • Chengannur, Kerala
    • Posts 31
    • Points 485
    Re: How to parse the contents of a file? Reply

    Hi,

    Something like this would do..

     procedure( parseFile( ipFile )
      let( ( tokens table )
        table = list()
        fpFile = infile( ipFile )
        while( gets( line fpFile )
          tokens = parseString( line )
          tokens && ( table = cons( tokens table ) )
        ) ;while file
        close( fpFile )
        println( table )
      ) ;let
    ) ;procedure

     After saving the above code in a file, you need to load it in icfb.

    load( "parseFile.il" )
    parseFile( "int.txt" )

     

    Filed under:
    • Post Points: 20
  • Tue, Nov 16 2010 3:49 AM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Thanks LeJonT,

       It works but it returns the contents of file as list.Can i obtain each element of this list and outfile (instead of geeting it printed in CIW) it into another file say out.txt.

      my out.txt should look like in table form as,

       headers: <ruleno.>   <layername>   <type>    <value>

                       R1A          POLY           WIDTH      0.5

                       R1B          ACTIVE         WIDTH     0.4               

                      .....            .........           .........     ......

                    .......            ...........         .........     ........

    Thanks once again for your help,

       Messi

    • Post Points: 20
  • Tue, Nov 16 2010 4:15 AM

    • LeJonT
    • Top 500 Contributor
    • Joined on Thu, Apr 1 2010
    • Chengannur, Kerala
    • Posts 31
    • Points 485
    Re: How to parse the contents of a file? Reply

    Hi,

    This would do it.. 

    procedure( parseFile( ipFile outFile )
      let( ( tokens table fpFile fpoutFile )
        table = list()
        fpFile = infile( ipFile )
        fpoutFile = outfile( outFile )
        fprintf( fpoutFile "%-15s%-15s%-15s%-15s\n" "RULE NO" "LAYERNAME" "TYPE" "VALUE" )
        while( gets( line fpFile )
          tokens = parseString( line )
          tokens && fprintf( fpoutFile "%-15s%-15s%-15s%-15s\n" nth( 0 tokens ) nth( 1 tokens ) nth( 2 tokens ) nth( 3 tokens ) )
        ) ;while file
        close( fpoutFile )
        close( fpFile )
      ) ;let
    ) ;procedure

    parseFile( "int.txt" "out.txt" )

     But, I'm little surprised about why you are using SKILL for a file operation!

     

    Filed under:
    • Post Points: 20
  • Tue, Nov 16 2010 4:35 AM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Thanks alot,

        i wanted to know how i can read any file and out it.i tried to code by reading file as u have written using infile then parsing it.To make table i was using makeTable commands and i was using car and cdr concepts to get the contents of my parsed file.But my code generateed errors...

    anyways thanx for the help,

    Regards,

      Messi

      

    • Post Points: 20
  • Tue, Nov 16 2010 10:03 PM

    • LeJonT
    • Top 500 Contributor
    • Joined on Thu, Apr 1 2010
    • Chengannur, Kerala
    • Posts 31
    • Points 485
    Re: How to parse the contents of a file? Reply

    Hi,

      You ofcourse can choose to use table, list or any datatype that suits the purpose. Here in this case, double list can serve the purpose. car and nth( 0 list ) are similar and car is much more efiiicient to.

    If you can post the code in here, we can try to see to the issues..

     

    Filed under:
    • Post Points: 20
  • Wed, Nov 17 2010 3:38 AM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Thanks for your help,

        This is working and able to write to outfile.But can i know how to do the same using makeTable command.ie..my input would be just a rule file with the format mentioned earlier.my output would be a SKILL table.

    eg: a = makeTable("myTable" nil)

         so when i say a[R1A] it should return a list as: ("POLY" "WIDTH" "0.5")

    Thanks,

    Messi

    • Post Points: 20
  • Wed, Nov 17 2010 7:03 AM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: How to parse the contents of a file? Reply

    This is simple enough. Here is the original code suggestion with changes to make it store the data in a table:

    procedure( parseFile( ipFile )
      let( ( tokens table )
        table = makeTable("ruleTable" nil)
        fpFile = infile( ipFile )
        while( gets( line fpFile )
          tokens = parseString( line )
          when(tokens
            table[car(tokens)] = cdr(tokens)
          )
        ) ;while file
        close( fpFile )
        printstruct( table )
      ) ;let
    ) ;procedure

    Derek

    • Post Points: 20
  • Wed, Nov 17 2010 9:37 PM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Hi Derek,

             Thanks for your help.But when my rule file have this

                         RULE NO.    LAYER       TYPE                    VALUE
                            R1A,          POLY,      POLY WIDTH,            0.5
                            R1B,          POLY,      POLY SPACING,        0.25
                            R2A,          ACTIVE,    ACTIVE WIDTH,        0.7
                            R2B,         ACTIVE,    ACTIVE SPACING,     0.3

    when i load the code the output would have on top of it the headers ie (RULE NO.,LAYER...etc). cdr would help me remove these headers right??the output is also found to have results with R1B occuring first then R1A.How can get the output in same order as in the input rule file??

     

    • Post Points: 5
  • Wed, Nov 17 2010 10:36 PM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Hi,

    Also i found my output to be in format like:

       "R2B:  ("          ACTIVE" "   ACTIVE SPACING  0.3\n")

    How can i make my ouput luk sumthng like:

       "R2B:   ACTIVE   ACTIVE SPACING   0.3\n"

    Is it done by getting length of parsed output and then removing brackets??This is just to make my output luk better and better formatted.

    Regards,

    Messi

    • Post Points: 5
  • Thu, Nov 18 2010 2:52 AM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Hi,

     in my input code i used comma as my delimiter.i used parseString( line "," ) and i found the output as i mentioned above...I am finding trouble in formatting it.

    Thanks,

    Messi

    • Post Points: 20
  • Thu, Nov 18 2010 7:19 AM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: How to parse the contents of a file? Reply

    Messi,
    Your requests are not consistent. This formum is not intended for coding solutions to meet everyone's needs, we are simply here to provide help and suggestions to get you started. Your original posts did not show you had commas in your input. One of your other posts indicated you just wanted to write the data back to a file. Yet another post said you wanted the data in a table. Now it becomes apparent that you want your data in a table, you want to write it to a file in nearly the same format as it came in with better tabulation and now your input has commas. You also mentioned your code was failing but never pasted any code.

    Some comments based on your last three posts:
    If you want to throw out your header line you can add a line before you add data to the table:
         when(tokens
            unless(car(tokens)=="RULE"
               table[car(tokens)] = cdr(tokens)
            )
          )


    Tables do not maintain order like lists do, but they are much more efficient. If your rules are in alphabetical order, then they can be sorted before you print them out. I simply used the printstruct command to show the contents of the table. If you want to write the data back out to a file in a "cleaner" format, you can write it out as you read it in AND store it in a table to be used later in your session. If you want to loop through the table and write the data out, the best you can do is sort the keys of the table.
    foreach(key sort(table~>? 'alphalessp)
        printf("%-10s:    %L\n" key table[key])
    )

    As far as the format of the output, when you started using a parseString with commas, you end up leaving all of the spaces in the data. Doing a parseString on the whitespace (no arguments after the string), will get rid of the extra space, but will split stuff like "POLY WIDTH" into two separate list items. If all you are doing is reading in a text file and writing it back out, Skill is NOT the ideal language for this. In this case, if you must use commas, you'll probably want to get familiar with commands like rexCompile and rexReplace so that you can remove leading spaces.

    a="    MY RULE"
    rexCompile("^ *")
    b=rexReplace(a "" 0)

    Another option is to use: buildString(parseString(a))
    This parses out the spaces and rebuilds the string with only one space between each word.

    Example:
    rexCompile("^ *")
    foreach(key sort(table~>? 'alphalessp)
        printf("%s:\t" key)
        foreach(col table[key]
            col = rexReplace(col "" 0)
            printf("%-15s \n" col)
        )
        printf("\n")
    )

    So a final solution could be:
    procedure(parseFile( @optional (ipFile "/tmp/rule.txt" ))
      let( ( tokens table fpFile value key line)
        table = makeTable("ruleTable" nil)
        fpFile = infile( ipFile )
        while( gets( line fpFile )
          tokens = parseString( line ",")
          when(tokens
            value = nil
            unless(rexMatchp("RULE NO" car(tokens))
              rexCompile("^ *")
              key = rexReplace(car(tokens) "" 0)
              foreach(col cdr(tokens)
                  col = buildString(parseString(col))
                  value = append1(value col)
              )
              when(key && value
                  table[key] = value
              )
            )
          )
        ) ;while file
        close( fpFile )
        foreach(key sort(table~>? 'alphalessp)
            printf("%s:  " key)
            foreach(col table[key]
                printf("%-15s " col)
            )
            printf("\n")
        )
        table
      ) ;let
    ) ;procedure

    Derek

    • Post Points: 20
  • Thu, Nov 18 2010 8:25 PM

    • Messi
    • Top 150 Contributor
    • Joined on Thu, Nov 11 2010
    • Posts 49
    • Points 770
    Re: How to parse the contents of a file? Reply

    Hi, thanks for the help

     My code was this: This had space as delimiter .

    procedure( parseFile( fileName )
    let( (ruleFile line token tmpList Ctr wordlist)
     
        unless( ruleFile = infile( fileName)
          error( "Cannot open file %L for reading" fileName)
        ) ; unless
        
        Table = makeTable( "MY_TABLE" nil)
        Ctr = 1
        token = car( line)
        tmpList = cons( token tmpList)
        Table[token] = list( "list(")
        gets( line ruleFile)
        Ctr = Ctr + length(
      setof( paren parseString( line "") paren == "("))
        Ctr = Ctr - length(
      setof( paren parseString( line "") paren == ")")) 
      
        wordList = parseString( line " \n\t")
         while( Ctr != 0 && line
             Table[token] = cons( line Table[token])
                    gets( line ruleFile)
        wordList = parseString( line||"" " \n\t")
            Ctr = Ctr + length(
          setof( paren parseString( line||"" "") paren == "("))
             Ctr = Ctr - length(
        setof( paren parseString( line||"" "") paren == ")"))
           ) ; while 
         if( Ctr == 0 then
             Table[token] = reverse( cons( line
               Table[token]))
           else
      
      Table[token] = "ERROR "
           ) ; if
          );list      
          
          
      
        close(ruleFile)
        list( reverse( tmpList) Table )
      
     
       );let
     );procedure
       
    anyway thanks, i was experimenting with my input file and trying to bring changes in my code to as i felt that is the best way to learn.

     

    Regards,

    Messi

    • Post Points: 5
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts:
Started by Messi at 15 Nov 2010 11:04 PM. Topic has 15 replies.