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

Hello, I imagine there is an easy answer to this question, but I can't seem to find it by searching the site. I had a set of files with a naming pattern like this: "CAC1Bvariant$variable_fasta.txt"

I'm trying to read all the files and iterate over the digit range, but perl keeps interpreting the $variable as extending all the way to the end of the file name. I can't figure out what character could be used to terminate the variable name. Thank you for reading this. Evan

Replies are listed 'Best First'.
Re: Variable in a string
by chromatic (Archbishop) on Dec 09, 2011 at 18:47 UTC

    Good question! Use curly braces: my $pattern = "CAC1Bvariant${variable}_fasta.txt";.


    Improve your skills with Modern Perl: the free book.

Re: Variable in a string
by Old_Gray_Bear (Bishop) on Dec 09, 2011 at 19:15 UTC
    You can build your name in stages:
    my $file_name = "CAC1Bvariant"; $file_name .= $variable; $file_name .= '_fasta.txt';
    I think it's a little easier on the maintenance programmer's eyes, and it high-lights the fact that the varying part of the name is in the middle of the string.

    ----
    I Go Back to Sleep, Now.

    OGB

      Sleepy Bear,

      Might this be even easier on the eyes?

      my $file_name = 'CAC1Bvariant' . $variable . '_fasta.txt';
        You guys are awesome. I've used this site for 10 years and have valued it every time I've asked a question (though haven't in 8 years, and forgot my user info!)
Re: Variable in a string
by toolic (Bishop) on Dec 09, 2011 at 18:57 UTC
Re: Variable in a string
by leuchuk (Novice) on Dec 09, 2011 at 20:45 UTC

    Could it be that you read a directory with these file names and do something with these file names?

    Use single quotes or q{} for such file names.