CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
filenames

 



adp
newbie

Apr 19, 2001, 6:46 AM

Post #1 of 2 (390 views)
filenames Can't Post

simple simple question:

i've declared a variable
$item_XYZ = apple

and i want to name an output file to be this
$item_XYZ_mond.txt :
apple_mond.txt

and another output file to be called
$item_XYZ_4.txt
apple_4.txt

how can I do this?





localfilmmaker
stranger

Apr 19, 2001, 12:09 PM

Post #2 of 2 (386 views)
Re: filenames [In reply to] Can't Post

Well, first you would need to start out with quotes like this:
$item_XYZ = "apple";

You can use a variable in a string like this "I like $item_XYZ" and get "I like apple", because the variable gets interpolated. But as you've probably noticed, you can't do "I like $item_XYZ_4" and get "I like apple_4". The problem is because it thinks that $item_XYZ_4 is a variable. The way around this is to enclose the variable name in curly braceslike this "I like ${item_XYZ}_4". Here's how to open a file using this technique:

$item_XYZ = "apple";
open(FILE, "> ${item_XYZ}_4.txt") or die "can't write to file: $!";

Now, only the ${item_XYZ} is considered a variable, and the rest is just text.

-localfilmmaker
Spencer Christensen
spencer@mecworks.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives