Question:
perl program?
anonymous
2008-06-02 08:26:35 UTC
Code:
open (NGC_Input_File,"ngc_update.xml");

Problem:
I'm new in perl and i have a code above. but i wanted to add where it will pick up any file that starts with ngc (ngc*.xml). I tris experimenting by adding * and othre stuff but it wont work plase help??

Also is it possible to add a directory. Let say \perl\bin in the code? if so how?
Three answers:
DaveE
2008-06-02 09:10:02 UTC
This question (your question!) got answered pretty well just the other day:



https://answersrip.com/question/index?qid=20080530223212AA5vVk6



Otherwise, to basically repeat what was said, the best way of doing what you want would be something like:



$dir = "/whatever/dir/you/want"; #or "C:\whatever\dir" if you're in Windows

if(opendir(DIR,$dir)) {

foreach my $filename (readdir DIR) {

if($filename =~ /ngc.*\.xml$/i) {

if(open(NGC_Input,"$dir/$filename")) {

#do whatever you want

close (NGC_Input);

} else {

print "Could not open $dir/$filename!\n";

}

}

}

} else {

print "Couldn't open $dir!\n";

}



#Of course, switch the / for \ if you're using windows where you reference $dir/$filename!



DaveE
anonymous
2008-06-02 15:38:40 UTC
You can't open any file, you have to find any file, then open a specific file among those you found. That's not Perl, that's programming - in any language.



To add a directory, just add it:



open (NGC_Input_File,"/perl/bin/ngc_update.xm...
petdance
2008-06-04 01:52:24 UTC
Please note that on Windows, "C:\whatever\dir" is invalid. You want either



'C:\whatever\dir'



or



"C:\\whatever\\dir"



or best yet



'C:/whatever/dir'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...