Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 243517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:55:40+00:00 2026-05-11T20:55:40+00:00

I have a header file in which there is a large struct. I need

  • 0

I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.

For example I have some structure like

const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,54,
55,58,60,64,65,66,67,69,
70,72,76,77,81,82,83,85,
88,93,94,95,97,99,102,103,
105,106,113,115,122,124,125,126,
129,131,137,139,140,149,151,152,
153,155,158,159,160,163,165,169,
174,175,181,182,183,189,190,193,
197,201,204,206,208,210,211,212,
213,214,215,217,218,219,220,223,
225,228,230,234,236,237,240,241,
242,247,249};

Now, I need to read this and apply some operation on each of the member variable and create a new structure with different order, something like:

const BYTE Some_Idx_Mod_mul_2[] = {
8,14,20, ...
...
484,494,498};

Is there any Perl library already available for this? If not Perl, something else like Python is also OK.

Can somebody please help!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:55:41+00:00Added an answer on May 11, 2026 at 8:55 pm

    Keeping your data lying around in a header makes it trickier to get at using other programs like Perl. Another approach you might consider is to keep this data in a database or another file and regenerate your header file as-needed, maybe even as part of your build system. The reason for this is that generating C is much easier than parsing C, it’s trivial to write a script that parses a text file and makes a header for you, and such a script could even be invoked from your build system.

    Assuming that you want to keep your data in a C header file, you will need one of two things to solve this problem:

    • a quick one-off script to parse exactly (or close to exactly) the input you describe.
    • a general, well-written script that can parse arbitrary C and work generally on to lots of different headers.

    The first case seems more common than the second to me, but it’s hard to tell from your question if this is better solved by a script that needs to parse arbitrary C or a script that needs to parse this specific file. For code that works on your specific case, the following works for me on your input:

    #!/usr/bin/perl -w
    
    use strict;
    
    open FILE, "<header.h" or die $!;
    my @file = <FILE>;
    close FILE or die $!;
    
    my $in_block = 0;
    my $regex = 'Some_Idx\[\]';
    my $byte_line = '';
    my @byte_entries;
    foreach my $line (@file) {
        chomp $line;
    
        if ( $line =~ /$regex.*\{(.*)/ ) {
            $in_block = 1;
            my @digits = @{ match_digits($1) };
            push @digits, @byte_entries;
            next;
        }
    
        if ( $in_block ) {
            my @digits = @{ match_digits($line) };
            push @byte_entries, @digits;
        }
    
        if ( $line =~ /\}/ ) {
            $in_block = 0;
        }
    }
    
    print "const BYTE Some_Idx_Mod_mul_2[] = {\n";
    print join ",", map { $_ * 2 } @byte_entries;
    print "};\n";
    
    sub match_digits {
        my $text = shift;
        my @digits;
        while ( $text =~ /(\d+),*/g ) {
            push @digits, $1;
        }
    
        return \@digits;
    }
    

    Parsing arbitrary C is a little tricky and not worth it for many applications, but maybe you need to actually do this. One trick is to let GCC do the parsing for you and read in GCC’s parse tree using a CPAN module named GCC::TranslationUnit.
    Here’s the GCC command to compile the code, assuming you have a single file named test.c:

    gcc -fdump-translation-unit -c test.c

    Here’s the Perl code to read in the parse tree:

      use GCC::TranslationUnit;
    
      # echo '#include <stdio.h>' > stdio.c
      # gcc -fdump-translation-unit -c stdio.c
      $node = GCC::TranslationUnit::Parser->parsefile('stdio.c.tu')->root;
    
      # list every function/variable name
      while($node) {
        if($node->isa('GCC::Node::function_decl') or
           $node->isa('GCC::Node::var_decl')) {
          printf "%s declared in %s\n",
            $node->name->identifier, $node->source;
        }
      } continue {
        $node = $node->chain;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 205k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only option to do it directly is Reflection all… May 12, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer What about $obj['name'] ? For instance, if you take this… May 12, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer Hopefully there aren't too many hidden, hidden features - but… May 12, 2026 at 9:04 pm

Related Questions

I'm working on a C++ project in which there are a lot of classes
I have a rather large project I'm porting, and in one of the MANY
I have a set of assembly function which I want to use in C
I am writing a library in standard C++ which does the phonetic conversion. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.