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

RE: [PBML] searching for pattern maching #ifdef or #ifndef in c source file

Expand Messages
  • Jeff Eggen
    ... The operator only returns one line from the file in scalar context. You only have one line of the file in your $inline variable. If you want to print
    Message 1 of 4 , Nov 5, 2004
    • 0 Attachment
      >open(INFILE,$ARGV[0]);
      >$inline=<INFILE>;
      >if ($inline =~ /^#ifdef/){
      >print $&;}
      >elsif($inline =~ /^#ifndef/){
      >print$&;}
      >else{print("ther are not more symboles.\n");}

      The <> operator only returns one line from the file in scalar context. You
      only have one line of the file in your $inline variable. If you want to
      print out the lines that start with #ifdef or #ifndef, you could just cycle
      through the file one line at a time:

      # Begin code
      while (<>) # perldoc perlop
      {
      print if (m/^#ifn?def); # perldoc -f print, perldoc perlretut
      }
      print "There are no more symbols.\n";
      # End code

      Since the input file(s) are listed on the command line as arguments, you can
      do an implicit open on them, and since the two strings you are searching for
      are very close you can get by with one pattern that will match either one.
      This solution also avoids using $&, which as perlvar will tell you is a
      large performance hit. Anyhoo, read up on the perldocs I listed above, and
      it should all make sense.

      Hope this helps,
      Jeff Eggen
    • Allan Dystrup
      ... source file. I tried out following ... ... #!/usr/bin/perl use strict; use warnings; foreach ( ) { print if m/^#if(n|)def/; } __END__ #ifdef or
      Message 2 of 4 , Nov 5, 2004
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, Nicck_par <nicck_par@y...>
        wrote:
        > Hi all,
        >
        > I want to find all the occurance of #ifdef and #ifndef in a c-
        source file. I tried out following ...
        >
        > open(INFILE,$ARGV[0]);
        > $inline=<INFILE>;
        > if ($inline =~ /^#ifdef/){
        > print $&;}
        > elsif($inline =~ /^#ifndef/){
        > print$&;}
        > else{print("ther are not more symboles.\n");}
        >
        >
        > But its not working...Any comments...!!!
        >
        > Nicck.
        >

        #!/usr/bin/perl

        use strict;
        use warnings;

        foreach ( <DATA> ) {
        print if m/^#if(n|)def/;
        }
        __END__
        #ifdef
        or
        #ifndef
        or
        #ifdef
        or
        somethingelse


        >
        > ---------------------------------
        > Do you Yahoo!?
        > Check out the new Yahoo! Front Page. www.yahoo.com/a
        >
        > [Non-text portions of this message have been removed]
      • Nicck_par
        Thanks alot..!!!! can i also transfer these symbols and its values to the excel format...!!!! Bye, Nicck ... The operator only returns one line from the
        Message 3 of 4 , Nov 5, 2004
        • 0 Attachment
          Thanks alot..!!!!

          can i also transfer these symbols and its values to the excel format...!!!!

          Bye,
          Nicck

          Jeff Eggen <jeff.eggen3@...> wrote:
          >open(INFILE,$ARGV[0]);
          >$inline=<INFILE>;
          >if ($inline =~ /^#ifdef/){
          >print $&;}
          >elsif($inline =~ /^#ifndef/){
          >print$&;}
          >else{print("ther are not more symboles.\n");}

          The <> operator only returns one line from the file in scalar context. You
          only have one line of the file in your $inline variable. If you want to
          print out the lines that start with #ifdef or #ifndef, you could just cycle
          through the file one line at a time:

          # Begin code
          while (<>) # perldoc perlop
          {
          print if (m/^#ifn?def); # perldoc -f print, perldoc perlretut
          }
          print "There are no more symbols.\n";
          # End code

          Since the input file(s) are listed on the command line as arguments, you can
          do an implicit open on them, and since the two strings you are searching for
          are very close you can get by with one pattern that will match either one.
          This solution also avoids using $&, which as perlvar will tell you is a
          large performance hit. Anyhoo, read up on the perldocs I listed above, and
          it should all make sense.

          Hope this helps,
          Jeff Eggen



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


          Yahoo! Groups SponsorADVERTISEMENT


          ---------------------------------
          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!?
          Check out the new Yahoo! Front Page. www.yahoo.com/a

          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.