• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. How to parse the contents of a file?

Stats

  • Locked Locked
  • Replies 15
  • Subscribers 145
  • Views 24587
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to parse the contents of a file?

Messi
Messi over 14 years ago

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

  • Cancel
Parents
  • dmay
    dmay over 14 years ago

    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

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • dmay
    dmay over 14 years ago

    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

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information