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

$CapitalieFirstLetter =~ s/^([a-z])/\u$1/; Can anyone see why this doesn't work?!
hopefully one of those simple stupid things.

cheers
ant

Replies are listed 'Best First'.
Re: Daft Regexp
by Cody Pendant (Prior) on Aug 05, 2003 at 00:48 UTC
    Maybe it's the inherent inefficiency of not using ucfirst() to, er, uc the first character that's causing it to fail?

    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Daft Regexp
by bart (Canon) on Aug 05, 2003 at 01:32 UTC
    Perhaps the first character in your string isn't a letter. As apparently it's supposed to be a directory path, it likely isn't.
      Yup that's it
      i was regexping the path with file, not the file then adding on the path so
      my ($newname) =$handle."\\".$entries[$i]; $newname =~ s/^([a-z])/\u$1/g; rename ($handle."\\".$entries[$i], $newname);

      becomes
      my ($newname) =$entries[$i]; $newname =~ s/^([a-z])/\u$1/g; rename ($handle."\\".$entries[$i], $handle."\\".$newname);

      cheers for that, it's always simple things that mess up the most!
        What about what Cody Pendant carefully hinted at?
        my $newname = ucfirst $entries[$i]; rename ($handle."\\".$entries[$i], $handle."\\".$newname);

        Makeshifts last the longest.

      not sure which machine you're using...on unix boxen at least, if posix isn't setup properly on the box, then perl can realy balk at char conversions on e.g. unicode or mixed char set strings. why not print all the file/dir names to a file for inspection with a good ascii editor that shows you special chars etc, eg vi.
Re: Daft Regexp
by fourmi (Scribe) on Aug 05, 2003 at 00:47 UTC
    Forgot to log in initially
    #!wperl use IO::File; use strict; my $dir = $ARGV[0]; chomp($dir); $dir =~ s{\\}{\\\\}g; Recurse($dir); sub Recurse { my $handle = shift; opendir(SPROUT,$handle); my @entries = readdir(SPROUT); closedir(SPROUT); foreach my $i (2..scalar(@entries)) { my $param_handle = $handle."\\".$entries[$i]; if(opendir(TEST,$param_handle) and $entries[$i]) { closedir(TEST); $handle =~ s{\\\\}{\\}g; my ($newdirname)= $handle."\\".$entries[$i]; $newdirname =~ s/^([a-z])/\u$1/g; rename ($handle."\\".$entries[$i], $newdirname); Recurse($newdirname); } elsif ($entries[$i]) { my ($newname) =$handle."\\".$entries[$i]; $newname =~ s/^([a-z])/\u$1/g; rename ($handle."\\".$entries[$i], $newname); } } }

    And it doesn't seem to capitalise the first letter of dirs, and files... Could it be something else in the script?
    cheers
    ant
      The problem is not the regex. Take a look at the values of $newname and $newdirname and the error messages. Look at the ouptut after adding

      rename ... or die "Error renaming $newname: $!"; #use $newdirname where appropriate

      to each rename statement.

      Since you are renaming _every_ file and directory, the whole rename sequence is practically begging to be taken out of the if/elsif statement.

      PS: You probably want to take a look at File::Find

      Try using File::Find and doing a depth first search, its usually better practice (less error prone) to start renaming from the deepest part of the tree twords the top IMHO.

      -Waswas
Re: Daft Regexp
by allolex (Curate) on Aug 05, 2003 at 00:50 UTC

    Well, without the context, we'll probably be shooting in the dark. Put if I am guessing correctly from your variable name, the following should demonstrate something that will work for you:

    perl -e'$text="now is the time for all good women."; $text =~ s/^([a-z +])/\u$1/;print $text . "\n";print $1 . "\n"' # outputs 'Now is the time for all good women.' and then 'n' on the ne +xt line

    You are printing afterwards, right? :)

    --
    Allolex

    Update 2003-08-05 02:52:32 CEST: I can't believe I spent more than 15 minutes on this node... I think I'll go sleep.

      Hehe
      yup printing out afterwards, that sems pretty similar to the steps i am taking, so i assume there's nothing inherintly (sp?) wrong with my script, just a buggy bit somewhere...
      cheers for you help!
Re: Daft Regexp
by Zaxo (Archbishop) on Aug 05, 2003 at 00:35 UTC

    In what way doesn't it work for you? Seems ok to me as far as this goes,

    $ perl -e'$_="foo";s/^([a-z])/\u$1/;print' Foo$

    After Compline,
    Zaxo

Re: Daft Regexp
by BrowserUk (Patriarch) on Aug 05, 2003 at 00:39 UTC

    No, but then it does work for me.

    $_='fred'; s/^([a-z])/\u$1/; print; Fred

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.