Welcome To Support Community

Materials Studio

Advanced Search
Ask Search:
DawnDawn 

Using perl script within GUI to extract lines from within cif files

Hello,

I'm new to Perl and am not sure whether I can do what I intend so would greatly appreciate some advice please.

Within Materials Studio (2016) GUI I want to use perl to open some cif files within a directory held outside of a  Materials Studio project, and then extract a particular line from each cif file, using this line in a subsequent calculation, also contained within the perl script.

I don't understand why the following fails at   open (my $fh, '<', $file2) or die $!; (The following is part of a larger code)
####################################################:

my $i = 0;
my $input_suffix = 'cif';
opendir my $folder, $inputFilesFolder or die "Couldn't open folder";
my @allfiles = grep  /^.+\.$input_suffix$/, readdir $folder;
print "@allfiles";
print "This is the main folder: $folder";
foreach my $file (@allfiles) {
     print "Parsing $file \n";

print "This is the list of cif files:@allfiles\n";
print "This is the number of elements in '@allfiles': " . @allfiles . "\n";

my $prefix = "$folder";

#print "$files[2]\n";




my @filepaths = "$outputFilesFolder/$file";
# my @filepaths =  map { $prefix . $_ } @allfiles; 

print "This is now the new list of filepaths: @filepaths";



print "Size: ",scalar @filepaths, "\n";
print "This is each cif file with its full path:@filepaths \n";

print "# This is the number of Elements: " . @filepaths . "\n";
   

#closedir $folder;
print "This is the directory we're now in: $folder \n";


foreach my $file2(@filepaths) {
   print "This is each file and its path again: $file \n";
    print "# Elements: " . @filepaths . "\n";
    open (my $fh, '<', $file2) or die $!;

   
    my @lines = <$fh>;
       for (@lines) {

        

    if ($_ =~ /(z_value)\s\:\s(\d+\.\d+)/) {
        print "$2\n";
     }
   }


  }
}
######################################

Am I trying to do the impossible within the GUI?

Advice welcome!

Many thanks and best wishes,
Dawn Geatches
 
Jason DeJoannisJason DeJoannis
Hi Dawn,

No it is not impossible. Opening and reading files in Perl is pretty standard, if a little frustrating sometimes. As this question is pure Perl and not Materials Studio related, I suggest you find a good internet reference on reading directories and files, eg,
https://perlmaven.com/reading-the-content-of-a-directory

Note there could be differences that depend on the version of Perl. You can print out the version with the line "print $];"