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

Hello All, Very new to perl here and I'm trying how to figure out how I can do a character match and character substitution on characters at the beginning of a sting. What I have is a file with a list of file names and I want to take all of the file names that start with "s" and change the name to start with "_S"? Here is the script that I have so far. It dies with syntax errors.
Thanks in advance for the help.
George
#!/usr/bin/perl -w use strict; my $fname; open FNAMES,"</var/tmp/flist"; while ( $fname = <FNAMES> ) { chomp ($fname) # Does the string start with an "s"? if ( /^$fname/ eq "s" ) { # how do I do the substitution?? printf "hello\n"; } }
  • Comment on How do I do character substitution on strings stored in a variable
  • Download Code

Replies are listed 'Best First'.
Re: How do I do character substitution on strings stored in a variable
by Andrew Coolman (Hermit) on Oct 03, 2008 at 17:48 UTC
    Instead:
    if ( /^$fname/ eq "s" ) { # how do I do the substitution?? printf "hello\n"; }
    Use:
    $fname =~ s/^s/_S/;

    Regards,
    s++ą  ł˝ ął. Ş ş şą Żľ ľą˛ş ą ŻĽąş.}++y~-~?-{~/s**$_*ee
      Thank you!
Re: How do I do character substitution on strings stored in a variable
by Fletch (Bishop) on Oct 03, 2008 at 17:51 UTC

    Your attempted comparison is way off. You're trying to use the filename you want to match against as a regular expression, the result of that comparison you're for some reason trying to compare with the string "s". You need to use the =~ operator to bind a match /^s/ to the variable holding the filename ($fname =~ /^s/; see perlretut). As for the substitution, you should look at the docs for the substitution operator. And unrelated, but you probably want print not printf.

    And you might look for some better introductory material (see http://learn.perl.org/ for example).

    Update: Actually Regexp Quote-Like Operators in perlop is probably a better source for info on s///.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How do I do character substitution on strings stored in a variable
by TGI (Parson) on Oct 03, 2008 at 19:30 UTC

    There are two major ways to ask the question "Does this string start with S?" in Perl. One method is to use a regular expression. The other is to use substr.

    #!/usr/bin/perl use strict; # glad to see you using strict. Good job! use warnings; # lexical warnings are better the the -w switch, # becuase you can turn them off where appropriate. # Good job using warnings, too. use diagnostics; # diagnostics give verbose explanations of error mess +ages. # It can be very handy when you are just starting out +. # You had: open FNAMES,"</var/tmp/flist"; # I made several changes here: # * I switched to the 3 argument open. It's avoids some potential # security issues, and is generally considered to be the best # way to go. # * I am also using a lexical filehandle instead of a global handle. # This is generally considered to be the best way to go as well, # for the same reasons that we prefer lexical variables with # limited scope to global variables. # * Finally, I am checking the results of the open() call. If the # call fails I end the program and display an error message. open( my $filenames, '<', '/var/tmp/flist' ) or die "Unable to open file list - $!\n"; while ( defined( my $fname = <$filenames> ) ) { chomp ($fname); # Added a semicolon # Regex based approaches: # Does the string start with an "s"? if ( $fname =~ /^s/ ) { $fname =~ s/^s/_S/; # Do the substitution # Do special stuff } # Did we replace a starting 's' with '_S'? if ( $fname =~ s/^s/_S/ ) { # Do special stuff if we did a substitution. } # Using substr() # # Does the string start with an "s"? if ( 's' == substr($fname, 0, 1) ) { substr($fname, 0, 1) = '_S'; # Do the substitution # or substr($fname, 0, 1, '_S'); # Do the substitution # Do special stuff } # Does the string start with an "s"? if ( 's' == substr($fname, 0, 1) ) { substr($fname, 0, 1) = '_S'; # Do the substitution # or substr($fname, 0, 1, '_S'); # Do the substitution # Do special stuff } }

    For this situation, I'd just do the substitution with a regex in the condtional test: if ($fname =~ s/^s/_S/) .... In fact, in most cases a regex is the way to go for looking into strings and finding stuff inside them. But sometimes substr is the way to go.

    You've made a good start. Keep working, and you will see good results.


    TGI says moo

Re: How do I do character substitution on strings stored in a variable
by toolic (Bishop) on Oct 03, 2008 at 19:05 UTC