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

i want to recurse throught a directory open all files with extension .c & .h, to find the Tabs in the files and replace it with 4 spaces. i knew the substitution code when edited in gvim

s/t/ /g

Replies are listed 'Best First'.
Re: recursive searching in directories
by moritz (Cardinal) on Nov 04, 2011 at 05:16 UTC

      i want to search for a ".c" file or ".h" file in a dir.so, i just took an example. this piece of code is giving unexpected output can you tell where it is going wrong

      $tu ="D:\\vroom\\diags\\in\\frameworks\\my_ip\\shock.h"; print "res:\t$res\n" if $res=$tu=~ m/$.c/g; print "res:\t$res\n" if $mes=$tu=~ m/$.h/g;

      this pice of code returning true for even the first case. it is just searching for "c" not ".c". what should i do to make it search for ".c". where can i check wether i received an answer to my post ?

      $tu ="D:\\vroom\\diags\\in\\frameworks\\my_ip\\shock.h"; if ($mes=$tu=~ m/$.h/g) { open FH, $tu or die "cannot open D:\\vroom\\diags\\in\\frameworks\ +\my_ip\\shock.h :$!\n"; while ( my $line = <FH>) { $line =~ s/\\t/ /g; } close (FH) or die "cannot close D:\\vroom\\diags\\in\\frameworks\\my_i +p\\shock.h:$!\n"; } else { print "out of loop\n"; }

      Above code snippet is not substituting \t(tab) with four space. what should i do achieve it ? How do i get intimated wether i received an answer to my post ?????? THANKS,

        $. is a special variable, and probably doesn't do what you think in the regex. You probably want m/\.c$ (and no g at the end).

        See perlretut for a gentle introduction to regular expressions.

Re: recursive searching in directories
by GrandFather (Saint) on Nov 04, 2011 at 07:40 UTC

    Of course if what you are trying to achieve is consistent indenting assuming four character tab positions what you propose won't work unless all tabs sequences start at tab positions already. If you have code such as the following (assume all white space consists of an appropriate number of tabs):

    int x = 1; int x1 = 2; int x12 = 3; int x123 = 4; int x1234 = 5; int x12345 = 6;

    then simply replacing tabs with four spaces gives:

    int x = 1; int x1 = 2; int x12 = 3; int x123 = 4; int x1234 = 5; int x12345 = 6;

    which most likely isn't what you want.

    True laziness is hard work