Loading ...
Sorry, an error occurred while loading the content.

Regarding searchin certain occurance in number of files...!!!!

Expand Messages
  • nikunj
    Hi all, I want to find occurance of #ifdef and #ifndef in a directory containing many c sourcefiles. It shuld print the result of each file plus a logfile
    Message 1 of 6 , Jan 3, 2005
    • 0 Attachment
      Hi all,

      I want to find occurance of #ifdef and #ifndef in a directory
      containing many c sourcefiles. It shuld print the result of each file
      plus a logfile which will contain all the acculated values, i mean
      total occurance of all the keys...!!!! Here is my logic but i think
      there is some infinite loop somewhere and also i m not getting what i
      want to do with this...!!!!

      print " enter the directory for testing.\n";
      $dir = <STDIN>;
      chomp($dir);

      @array1 = <$dir/*.c>;
      foreach $p(@array1){
      $p =~ s#.*/##;}
      print @array1;
      $len = @array1;

      $i=0;
      #for($i=0;$i<$len;$i++){
      LABLE1: open(IN,"$dir/$array1[$i]") || die " cant open file :$!\n";
      printf("%25s\n\n",$array1[$i]);
      while(<IN>){
      print $array1[$i];

      if(/^#ifn?def/){
      $_ =~ s/\s+$//;
      $var1=$_;
      chomp($var1);
      $list{$var1} +=1;}
      %modulelist = %list;
      if (eof() && $i<$len){ print "this is the end of file.\n";
      foreach $p( keys %list){
      delete $list{$p};}
      $i++;
      next LABLE1;}
      }


      while (($first,$last)=each (%list)){print "$first\t $last\t\n";}



      Please guide me...I know its totally wrong logic...If any one know
      how to apend hashtable please guide me.

      Regards,
      Nicck.
    • daymobrew@yahoo.com
      ... You should firstly turn on warnings and use strict. With warnings you would see that %modulelist is used only once. There is no need for the LABLE1 label.
      Message 2 of 6 , Jan 4, 2005
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "nikunj" <nicck_par@y...> wrote:
        >
        > Hi all,
        >
        > I want to find occurance of #ifdef and #ifndef in a directory
        > containing many c sourcefiles. It shuld print the result of each file
        > plus a logfile which will contain all the acculated values, i mean
        > total occurance of all the keys...!!!! Here is my logic but i think
        > there is some infinite loop somewhere and also i m not getting what i
        > want to do with this...!!!!
        >
        > print " enter the directory for testing.\n";
        > $dir = <STDIN>;
        > chomp($dir);
        >
        > @array1 = <$dir/*.c>;
        > foreach $p(@array1){
        > $p =~ s#.*/##;}
        > print @array1;
        > $len = @array1;
        >
        > $i=0;
        > #for($i=0;$i<$len;$i++){
        > LABLE1: open(IN,"$dir/$array1[$i]") || die " cant open file :$!\n";
        > printf("%25s\n\n",$array1[$i]);
        > while(<IN>){
        > print $array1[$i];
        >
        > if(/^#ifn?def/){
        > $_ =~ s/\s+$//;
        > $var1=$_;
        > chomp($var1);
        > $list{$var1} +=1;}
        > %modulelist = %list;
        > if (eof() && $i<$len){ print "this is the end of file.\n";
        > foreach $p( keys %list){
        > delete $list{$p};}
        > $i++;
        > next LABLE1;}
        > }
        >
        >
        > while (($first,$last)=each (%list)){print "$first\t $last\t\n";}
        >
        >
        >
        > Please guide me...I know its totally wrong logic...If any one know
        > how to apend hashtable please guide me.
        >
        > Regards,
        > Nicck.

        You should firstly turn on warnings and use strict.
        With warnings you would see that %modulelist is used only once.
        There is no need for the LABLE1 label.

        I'm not 100% sure what you are trying to store in the hash. The actual
        ifdef/ifndef line?

        Maybe you can provide some comments in your code or some pseudo code.
        I would expect you to do something like:

        Prompt for directory.
        foreach $filename ( <$dir/*.c> )
        {
        open $filename and look for ifdef/ifndef
        if found, $hash{ $filename } = $line
        # or some incrementing of the value.
        close $filename
        }

        # Print out files with ifdef/ifndef lines.
        foreach $filename ( keys %hash )
        {
        print "$filename - $hash{$filename}"
        }

        Without knowing exactly what you are trying to store in the hash
        I cannot suggest what to do when an ifdef/ifndef line is found.
      • Nicck_par
        hi, What u have suggested to me is right..!!! I have already tried this and is working fine. But problem comes when i wan to generat a log of total occurance.
        Message 3 of 6 , Jan 4, 2005
        • 0 Attachment
          hi,

          What u have suggested to me is right..!!! I have already tried this and is working fine.

          But problem comes when i wan to generat a log of total occurance.

          I can expain it like this...lets say in directory dir1 there are three files file1.c,file2.c and file3.c.
          now i want to find the occurance of #ifdef and #ifndef in each of the file separately. Thats ok.

          Also i need a total log that how many times in the whole directory i mean in all files...this is happening. So addition of all...!!!! How can i do this....!!! Is there any way that for each hash table of each file I can append to a new hashtable which will contain fulll details.

          Regards,
          Nicck.

          daymobrew@... wrote:

          --- In perl-beginner@yahoogroups.com, "nikunj" <nicck_par@y...> wrote:
          >
          > Hi all,
          >
          > I want to find occurance of #ifdef and #ifndef in a directory
          > containing many c sourcefiles. It shuld print the result of each file
          > plus a logfile which will contain all the acculated values, i mean
          > total occurance of all the keys...!!!! Here is my logic but i think
          > there is some infinite loop somewhere and also i m not getting what i
          > want to do with this...!!!!
          >
          > print " enter the directory for testing.\n";
          > $dir = <STDIN>;
          > chomp($dir);
          >
          > @array1 = <$dir/*.c>;
          > foreach $p(@array1){
          > $p =~ s#.*/##;}
          > print @array1;
          > $len = @array1;
          >
          > $i=0;
          > #for($i=0;$i<$len;$i++){
          > LABLE1: open(IN,"$dir/$array1[$i]") || die " cant open file :$!\n";
          > printf("%25s\n\n",$array1[$i]);
          > while(<IN>){
          > print $array1[$i];
          >
          > if(/^#ifn?def/){
          > $_ =~ s/\s+$//;
          > $var1=$_;
          > chomp($var1);
          > $list{$var1} +=1;}
          > %modulelist = %list;
          > if (eof() && $i<$len){ print "this is the end of file.\n";
          > foreach $p( keys %list){
          > delete $list{$p};}
          > $i++;
          > next LABLE1;}
          > }
          >
          >
          > while (($first,$last)=each (%list)){print "$first\t $last\t\n";}
          >
          >
          >
          > Please guide me...I know its totally wrong logic...If any one know
          > how to apend hashtable please guide me.
          >
          > Regards,
          > Nicck.

          You should firstly turn on warnings and use strict.
          With warnings you would see that %modulelist is used only once.
          There is no need for the LABLE1 label.

          I'm not 100% sure what you are trying to store in the hash. The actual
          ifdef/ifndef line?

          Maybe you can provide some comments in your code or some pseudo code.
          I would expect you to do something like:

          Prompt for directory.
          foreach $filename ( <$dir/*.c> )
          {
          open $filename and look for ifdef/ifndef
          if found, $hash{ $filename } = $line
          # or some incrementing of the value.
          close $filename
          }

          # Print out files with ifdef/ifndef lines.
          foreach $filename ( keys %hash )
          {
          print "$filename - $hash{$filename}"
          }

          Without knowing exactly what you are trying to store in the hash
          I cannot suggest what to do when an ifdef/ifndef line is found.







          Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html



          ---------------------------------
          Yahoo! Groups Links

          To visit your group on the web, go to:
          http://groups.yahoo.com/group/perl-beginner/

          To unsubscribe from this group, send an email to:
          perl-beginner-unsubscribe@yahoogroups.com

          Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


          __________________________________________________
          Do You Yahoo!?
          Tired of spam? Yahoo! Mail has the best spam protection around
          http://mail.yahoo.com

          [Non-text portions of this message have been removed]
        • daymobrew@yahoo.com
          ... and is working fine. ... three files file1.c,file2.c and file3.c. ... the file separately. Thats ok. ... mean in all files...this is happening. So addition
          Message 4 of 6 , Jan 4, 2005
          • 0 Attachment
            --- In perl-beginner@yahoogroups.com, Nicck_par <nicck_par@y...> wrote:
            > hi,
            >
            > What u have suggested to me is right..!!! I have already tried this
            and is working fine.
            >
            > But problem comes when i wan to generat a log of total occurance.
            >
            > I can expain it like this...lets say in directory dir1 there are
            three files file1.c,file2.c and file3.c.
            > now i want to find the occurance of #ifdef and #ifndef in each of
            the file separately. Thats ok.
            >
            > Also i need a total log that how many times in the whole directory i
            mean in all files...this is happening. So addition of all...!!!! How
            can i do this....!!! Is there any way that for each hash table of each
            file I can append to a new hashtable which will contain fulll details.
            >
            > Regards,
            > Nicck.
            >

            Can you type out what the desired output would look like (forget the
            code that would be involved).
            For a could of the occurances you simply have one count variable, set
            to 0 before opening the first file and incrementing every time
            ifdef/ifndef is found ($count++).
            For a file-by-file count set $hash{$file}=0 before opening the file
            and then increment for each occurance ($hash{$file}++).

            Are you looking to do something more than this?
          • Nicck_par
            Please chekout the following output of the script which is not what i require...!!! Enter the directory name: test file1: test/cmh_ccr.c #ifdef FAX_AND_DATA -
            Message 5 of 6 , Jan 4, 2005
            • 0 Attachment
              Please chekout the following output of the script which is not what i require...!!!

              Enter the directory name:
              test

              file1:
              test/cmh_ccr.c
              #ifdef FAX_AND_DATA - 19
              #ifdef UART - 8
              #ifdef FF_FAX - 7
              #ifndef CMH_CCR_C - 1
              #ifdef FF_TTY - 1
              #ifdef _SIMULATION_ - 1
              #ifdef FF_PSI - 4
              #ifdef FF_SAT_E - 1
              #ifdef WAP - 1
              #ifdef FF_TCP_IP - 9
              #ifdef AT_INTERPRETER - 4
              #ifdef SIM_TOOLKIT - 10

              file2:
              test/dl_state.c
              #ifdef FAX_AND_DATA - 19
              #ifdef UART - 8
              #ifdef FF_FAX - 7
              #ifndef CMH_CCR_C - 1
              #ifdef FF_TTY - 1
              #ifdef _SIMULATION_ - 1
              #ifdef FF_PSI - 4
              #ifdef FF_SAT_E - 1
              #ifdef WAP - 1
              #ifdef FF_TCP_IP - 9
              #ifdef AT_INTERPRETER - 4
              #ifdef SIM_TOOLKIT - 10
              #ifndef DL_STATE_C - 1
              Total:
              log:
              #ifdef FAX_AND_DATA - 19
              #ifdef UART - 8
              #ifdef FF_FAX - 7
              #ifndef CMH_CCR_C - 1
              #ifdef _SIMULATION_ - 1
              #ifdef FF_TTY - 1
              #ifdef WAP - 1
              #ifdef FF_SAT_E - 1
              #ifdef FF_PSI - 4
              #ifdef FF_TCP_IP - 9
              #ifdef AT_INTERPRETER - 4
              #ifdef SIM_TOOLKIT - 10
              #ifndef DL_STATE_C - 1

              If u look properly there each key and value are reapeating in each file. for example the match found in file1 are also reapiting in file2 and also adding its own match. The log file is correct in this case ...!!! But for fie2 i need only match of itself and not all match also from previous file.

              I guess u understood what i want...!!!

              Bye,
              Nicck.
              daymobrew@... wrote:

              --- In perl-beginner@yahoogroups.com, Nicck_par <nicck_par@y...> wrote:
              > hi,
              >
              > What u have suggested to me is right..!!! I have already tried this
              and is working fine.
              >
              > But problem comes when i wan to generat a log of total occurance.
              >
              > I can expain it like this...lets say in directory dir1 there are
              three files file1.c,file2.c and file3.c.
              > now i want to find the occurance of #ifdef and #ifndef in each of
              the file separately. Thats ok.
              >
              > Also i need a total log that how many times in the whole directory i
              mean in all files...this is happening. So addition of all...!!!! How
              can i do this....!!! Is there any way that for each hash table of each
              file I can append to a new hashtable which will contain fulll details.
              >
              > Regards,
              > Nicck.
              >

              Can you type out what the desired output would look like (forget the
              code that would be involved).
              For a could of the occurances you simply have one count variable, set
              to 0 before opening the first file and incrementing every time
              ifdef/ifndef is found ($count++).
              For a file-by-file count set $hash{$file}=0 before opening the file
              and then increment for each occurance ($hash{$file}++).

              Are you looking to do something more than this?






              Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html



              ---------------------------------
              Yahoo! Groups Links

              To visit your group on the web, go to:
              http://groups.yahoo.com/group/perl-beginner/

              To unsubscribe from this group, send an email to:
              perl-beginner-unsubscribe@yahoogroups.com

              Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.



              ---------------------------------
              Do you Yahoo!?
              Send holiday email and support a worthy cause. Do good.

              [Non-text portions of this message have been removed]
            • daymobrew@yahoo.com
              ... i require...!!! ... file. for example the match found in file1 are also reapiting in file2 and also adding its own match. The log file is correct in this
              Message 6 of 6 , Jan 4, 2005
              • 0 Attachment
                --- In perl-beginner@yahoogroups.com, Nicck_par <nicck_par@y...> wrote:
                >
                > Please chekout the following output of the script which is not what
                i require...!!!
                >
                > Enter the directory name:
                > test
                >
                > file1:
                > test/cmh_ccr.c
                > #ifdef FAX_AND_DATA - 19
                > snip
                >
                > file2:
                > test/dl_state.c
                > #ifdef FAX_AND_DATA - 19
                > snip
                > Total:
                > log:
                > #ifdef FAX_AND_DATA - 19
                > snip
                >
                > If u look properly there each key and value are reapeating in each
                file. for example the match found in file1 are also reapiting in file2
                and also adding its own match. The log file is correct in this case
                ...!!! But for fie2 i need only match of itself and not all match also
                from previous file.
                >
                > I guess u understood what i want...!!!
                >
                > Bye,
                > Nicck.

                I guess the total for FAX_AND_DATA should be 20, if the dl_state.c and
                cmh_ccr.c values were correct.

                I think you need a hash of hashes. I've never implemented this (done
                hash of arrays and array of hashes).
                Use 'perldoc perldsc' or look at:
                http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perldsc.html

                Your pseudo code might be something like:
                foreach $file ( <$dir/*.c> )
                {
                my %finds;
                open $file
                if ifdef/ifndef found
                {
                # Might have to check 'if defined $finds{$line}' first.
                $finds{$line}++
                $HoH{$all}{$line}++ # For total.
                }
                close $file
                $HoH{$group} = { %finds };
                }

                See the perldsc doc for how to access the keys for later printing.
              Your message has been successfully submitted and would be delivered to recipients shortly.