Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone advise how can I get just the header name by modifying the below code for the below given input?

INPUT:- #include "string.h" --> below code prints header #include <stdlib.h> --> below code prints header #include <sys/dispatch.h> --> not able to get the header name(dispatc +h.h) if ($line =~ /#include [<"](\S+\.h)[>"]/) { print "$1\n"; #not printing the header third case }

Replies are listed 'Best First'.
Re: Printing just the file name for all the cases
by GrandFather (Saint) on Mar 20, 2011 at 08:24 UTC
    use strict; use warnings; while (my $line = <DATA>) { if ($line =~ /#include [<"](\S+\.h)[>"]/) { print "$1\n"; #not printing the header third case } } __DATA__ #include "string.h" #include <stdlib.h> #include <sys/dispatch.h>

    Prints:

    string.h stdlib.h sys/dispatch.h

    which is what I understand you to want, so where's your problem?

    True laziness is hard work

      In the third case for sys/dispatch.h,I only want dispatch.h

        Modify the substitution to /#include [<"](?:\S+[\\\/])?(\S+\.h)[>"]/.

        True laziness is hard work
Re: Printing just the file name for all the cases
by moritz (Cardinal) on Mar 20, 2011 at 08:52 UTC
    use strict; use warnings; use File::Basename; while (my $line = <DATA>) { if ($line =~ /#include [<"](\S+\.h)[>"]/) { print basename($1), "\n"; } } __DATA__ #include "string.h" --> below code prints header #include <stdlib.h> --> below code prints header #include <sys/dispatch.h> --> not able to get the header name(dispatch +.h)
Re: Printing just the file name for all the cases
by Marshall (Canon) on Mar 21, 2011 at 03:58 UTC
    #!/usr/bin/perl -w use strict; while (my $line = <DATA>) { if ( my $include_file = ($line =~ /#include\s+[<"](.*?)[>"]/)[0] ) { print "$include_file\n"; } } #prints: #string.h #stdlib.h #sys/dispatch.h __DATA__ INPUT:- #include "string.h" --> below code prints header this is just a BS, nonsense line #include <stdlib.h> --> below code prints header #include <sys/dispatch.h> --> not able to get the header name(dispatc +h.h)
Re: Printing just the file name for all the cases
by Anonymous Monk on Mar 20, 2011 at 08:19 UTC
    my $pichi_nakodadaka = $1;
    Then match on $pichi_nakodadaka to get the filename
      Like this
      #!/usr/bin/perl -- use strict; use warnings; use Test::More qw' no_plan '; Main( @ARGV ); exit( 0 ); sub Main { Happy(\&pichi_nakodadaka, 'pichi_nakodadaka' ); Happy(\&pichi_nakodadaka2, 'pichi_nakodadaka2' ); } sub Happy { my( $ref, $name ) = @_; my @arg = ( [ '#include "string.h"', 'string.h' ], [ '#include <stdlib.h>', 'stdlib.h' ], [ '#include <sys/dispatch.h>', 'dispatch.h' ], ); for my $arg( @arg ){ my( $in, $expect ) = @$arg; my $got = $ref->($in); is($got, $expect , $name); } } sub pichi_nakodadaka { my( $in ) = @_; return $1 if $in =~ /#include [<"](\S+\.h)[>"]/ ; } sub pichi_nakodadaka2 { my( $in ) = @_; if( $in =~ /#include [<"](\S+\.h)[>"]/ ){ my $pichi_nakodadaka = $1; $pichi_nakodadaka =~ s!^.*?/!!g; return $pichi_nakodadaka; } } __END__ $ perl pm.894302.pl ok 1 - pichi_nakodadaka ok 2 - pichi_nakodadaka not ok 3 - pichi_nakodadaka # Failed test 'pichi_nakodadaka' # at pm.894302.pl line 33. # got: 'sys/dispatch.h' # expected: 'dispatch.h' ok 4 - pichi_nakodadaka2 ok 5 - pichi_nakodadaka2 ok 6 - pichi_nakodadaka2 1..6 # Looks like you failed 1 test of 6.

      Sure VP:-),you can also match $verri puka