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

  • Home
  • SEARCH
  • 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 8606195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:02:04+00:00 2026-06-12T03:02:04+00:00

I have a file which has the following data in it typedef INTEGER Id;

  • 0

I have a file which has the following data in it

typedef INTEGER Id;
typedef Integer32 Id;
typedef Integer32 Id;
typedef Integer32 Id;
typedef INTEGER Identifier;
typedef Integer32 Index;
typedef Unsigned32 Identifier;
typedef Integer32 Index;
typedef Unsigned32 TunnelId;
typedef Unsigned32 TunnelId;
const Unsigned32 maxValue = 65535;
const Integer32 Index_maxValue = 65535;        
const Unsigned32 maxValue = 4294967295;
const Unsigned32 Index_maxValue = 65535;

I need to select the variables in this below order:
If variable has …

  • INTEGER and Integer32 definitions – Integer32 has to be selected
  • Integer32 and Unsigned32 definitions – Unsigned32 has to be selected
  • INTEGER and Unsigned32 definitions – Unsigned32 has to be selected

Output expected :

typedef Integer32 Id;
typedef Unsigned32 Identifier;
typedef Integer32 Index;
typedef Unsigned32 TunnelId;
const Unsigned32 maxValue = 65535;
const Unsigned32 Index_maxValue = 65535;

I have written the below piece of code …
But its not yielding the above expected result.

@file2 = @file // full content of the file in an array
for ($i = 0; $i <= $#file; $i++) {
   $temp = $file[$i];
   $check = $file[$i];
   $check =~ s/^\s+//;

   @fields = split(/ /, $check);

   @grepNames = grep(/$fields[2]/, @file2);

   if($#grepNames >= 1) {

      for ($j = 1; $j <= $#file; $j++) {
         if( $file[$i] =~ /INTEGER/ && $file[$j] =~ /Unsigned32/ ) {
            push(@data, $file[$j]);
         }
         elsif( $file[$i] =~ /INTEGER/ && $file[$j] =~ /Integer32/ ) {
            push(@data, $file[j]);
         }
         elsif( $file[$i] =~ /Unsigned32/ && $file[$j] =~ /Integer32/ ) {
            push(@data, $file[i]);
         }
    }
}
  • 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-06-12T03:02:05+00:00Added an answer on June 12, 2026 at 3:02 am

    Despite the fact that you have accepted 0% of your questions (It would be very fair to raise that number), here is an analysis of your code, so that you can write better (and working) Perl the next time.

    General comments on writing Perl

    Discipline vs. Chaos

    Always use strict and use warnings, especially when you are trying to figure out why your script isn’t working or when you are new to Perl. Usually, these pragmas alert you about most stupid errors everybody makes once in a while.

    Perl doesn’t force you to write good code (and TIM TOWDTY), but most of the time you should stick to the strict subset of Perl unless you have a very good reason.

    Using strict implies that you have to declare all your variables with my, unless you have a very good reason. I think that declaring variables is a good thing.

    Perl is not C

    Most built-in functions do not need parens to delimit their arguments. I.e. split(/ /, $check) and split / /, $check are the same thing in most circumstances.

    Also, Perl seldomly needs the for(INIT; COMPARE; INCREMENT) loop construct, especially when the last part is $i++. Instead, you can use the foreach-syntax and a range:

    for my $i ($MIN .. $MAX)
    

    Why your code doesn’t work

    And: what bad idiom can be avoided

    I already pointed out that all variables should be declared with my and that there is a better loop syntax available.

    @file2 = @file // full content of the file in an array
    

    // doesn’t introduce a comment. It is the defined-or operator. Also, this statement isn’t terminated by a ;. This will confuse Perl, as the following for loop is part of the same statement—but this is invalid.

    Also, you don’t change the contents of either @file2 or @file, therefore making a copy unneccessary.

    $temp = $file[$i];
    

    You never use $temp.

    @grepNames = grep(/$fields[2]/, @file2);
    

    You use this grep to find out how many lines contain the same variable name. This is superflouus as you loop through all elements in @file again later on.

    if($#grepNames >= 1)
    

    What you have written is the question: Is the highest index in @grepNames larger than or equal to 1? whereas you probably meant Did we have more than one match?. Id write that as

    if (@grepNames > 1)
    

    However, this is mainly a stylistical comment.

    if( $file[$i] =~ /INTEGER/ ...
    

    Wait what? If $file[$i] contains INTEGER, so will $check or $temp. You can save yourself some typing when using a scalar instead of an array subscript.

    push(@data, $file[$j]);
    

    You push something onto @data even if you already have a line with the same variable name. Even worse, if @file contains n elements, then the inner for loop iterates over n – 1 elements, and you push something onto @data in most cases, making your algorithm O(n²)

      i \ j | INTEGER | Int32 | UInt32
    --------+---------+-------+-------
    INTEGER | -       | j     | j
    Int32   | i       | -     | j
    UInt32  | i       | i     | -
    

    Here is a table that states (according to your rules) which element should make it onto @data. You might want to compare this table to your if/elsifs and figure out if some cases may be missing.

    push(@data, $file[j]);
    

    You forgot the $ sigil before j.

    A better solution

    Your algorithm runs in O(n²), or rather O(n * (2n – 1)). However, your problem can be solved in O(n).

    I viewed the problem as:

    Each line in the input has a identifier and an associated weight.

    For the output, that line from the set of all lines with the same identifier is to be selected that has a minimal weight. If two or more lines have the same minimal weight, any of these minimal lines may be selected.

    The weight of a line depends on what keyword is the 2nd word in the line:

    Unsigned32 => 1,
    Integer32  => 2,
    INTEGER    => 3,
    

    If the second word is none of these keywords, an error should be thrown.

    The order of the first occurrences of each identifier is to be the same in both the input and the output.

    In my solution (shown below), I used the 1st and the 3rd word as identifier. If a line was already present in the output, I updated it if the current line had a lower weight.


    Edit: My Solution

    #!/usr/bin/perl
    
    use strict; use warnings;
    
    my @data;
    my %index;
    
    while (<DATA>) {
       $_ =~ s/^\s+//;
       my @fields = split /\s+/, $_, 3;
       @fields = (@fields[0 .. 1], split /(?=\W)/, $fields[2], 2);
       my ($class, $type, $name, $rest) = @fields;
       if (defined $index{$class}{$name}) {
          # we have a predecessor
          my $index = $index{$class}{$name};
          $data[$index][1] = (sort compareTypes $data[$index][1], $type)[0];
       } else {
          push @data, \@fields;
          $index{$class}{$name} = $#data;
       }
    }
    
    foreach (@data)  {
       my @fields = @$_;
       print "@fields[0..2]$fields[3]";
    }
    
    sub compareTypes {
       my %weight = (
          Unsigned32 => 1,
          Integer32  => 2,
          INTEGER    => 3,
       );
       my $weight_a = $weight{$a} // die "undefined type $a";
       my $weight_b = $weight{$b} // die "undefined type $b";
       return $weight_a <=> $weight_b;
    }
    
    __DATA__
    typedef INTEGER Id;
    typedef Integer32 Id;
    typedef Integer32 Id;
    typedef Integer32 Id;
    typedef INTEGER Identifier;
    typedef Integer32 Index;
    typedef Unsigned32 Identifier;
    typedef Integer32 Index;
    typedef Unsigned32 TunnelId;
    typedef Unsigned32 TunnelId;
    const Unsigned32 maxValue = 65535;
    const Integer32 Index_maxValue = 65535;        
    const Unsigned32 maxValue = 4294967295;
    const Unsigned32 Index_maxValue = 65535;
    

    Output:

    typedef Integer32 Id;
    typedef Unsigned32 Identifier;
    typedef Integer32 Index;
    typedef Unsigned32 TunnelId;
    const Unsigned32 maxValue = 65535;
    const Unsigned32 Index_maxValue = 65535;  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file which has data in the following format A B -----
I have a large text file ( data ) which has the following layout:
I have a text file which has the following data {i:1, j:2} {k:3, l:10}
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I have some binary data which has a corresponding map file which identifies each
I have a big (1.9 GB) XML file which has data I want to
I have a fixed width text file, which has been unpacked from Comp-3 data
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that
I have a PHP file which has the following text: <div class=small_italic>This is what

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.