in reply to Re: Loop is not working
in thread Loop is not working

Dear poj,

Thank you so much for your reply and precious time. I implemented your suggestion and here is the code:

#!/usr/bin/perl use strict; use warnings; # do first level statistical analysis run by run # go to the directory where the fsf-file for stats # and post-stats is stored chdir "/mnt/hgfs/ExpAutism/Scripts"; my $infile = 'Level1Run1_test.fsf'; my $outfile = 'Level1Run1_test_spec.fsf'; my $program = '/usr/local/fsl/bin/feat'; my @subjects = ("SubjectASD203","SubjectTD106C"); foreach my $subject (@subjects){ open my $fh_IN, '<', $infile or die "Could not open $infile ; $!"; open my $fh_OUT, '>', $outfile or die "Could not open $outfile ; $!"; # replace wildcards with specific titles of subjects while (<$fh_IN>){ s/Subjectx/$subject/g; print $fh_OUT $_; } close $fh_IN; close $fh_OUT; system($program,$outfile) == 0 or die "$program $outfile failed: $?"; } print "\n +++ DONE +++ \n";

The error message on the terminal:  C/usr/local/fsl/bin/feat Level1Run1_test_spec.fsf failed: 2 at Level1Run1_test.pl line 28. The line 28 is  system($program,$outfile) == 0

I am confused again. The problem seems to be with the feat file but what does "2" mean there? Do you have any suggestions?

PS1: The parent folder of each subject folder is /mnt/hgfs/ExpAutismand the parent folder of the fsf and pl files is  /mnt/hgfs/ExpAutism/Scripts

Thanks in advance. Best,

Replies are listed 'Best First'.
Re^3: Loop is not working
by Marshall (Canon) on Oct 04, 2018 at 01:14 UTC
    In general, I would not do a chdir from the Perl script and prefer to use a path name. But if you do a chdir, you should check for success.
    Did chdir  "/mnt/hgfs/ExpAutism/Scripts"; work? -> check the result of all file system operations.

    In general, I've found that changing the running default directory causes more problems than it fixes. Just a thought.

      I completely agree that absolute pathnames and writing scripts to be independent of the current working directory is a good thing. In addition to error checking, I'd also recommend using a module to make operations on filenames platform independent (File::Spec, Path::Class, Path::Tiny, etc.).

      Unfortunately, external programs aren't always written like that, and may depend on the current working directory (just one example of many would be git). In those cases, I like to use File::pushd to make changes to the current working directory temporary.

Re^3: Loop is not working
by haukex (Archbishop) on Oct 04, 2018 at 07:15 UTC
    die "$program $outfile failed: $?"; ... C/usr/local/fsl/bin/feat Level1Run1_test_spec.fsf failed: 2 at Level1Run1_test.pl ... what does "2" mean there?

    See $? and system for the interpretation of values of $?. In this case, it means that the program exited due to signal nr. 2, which normally corresponds to SIGINT, which normally corresponds to hitting Ctrl-C on your keyboard, and which would also explain the C at the beginning of your output (on my system, it shows as ^C).

    So it looks like you hit Ctrl-C while /usr/local/fsl/bin/feat was running. If so, why did you do that?

Re^3: Loop is not working
by NetWallah (Canon) on Oct 03, 2018 at 23:35 UTC
    How did "C" get pre-pended in the output:
    C/usr/local/fsl/bin/feat
    ? The assignment was :
    my $program = '/usr/local/fsl/bin/feat';
    which should have worked.

    If you need the drive letter, use "C:".

                    Memory fault   --   brain fried

Re^3: Loop is not working
by stevieb (Canon) on Oct 03, 2018 at 23:16 UTC

    This is a huge guess here, but I feel that this is due to a #define ENOENT 2 /* No such file or directory */ that may have been encountered.

    You may be in a directory that you think you are but aren't, or possibly trying to work on a file from a location where it doesn't exist. Can you change:

    system($program,$outfile) == 0 or die "$program $outfile failed: $?";

    ...to:

    print -x $program . "\n"; open my $fh, '<', $outfile or die $!;

    ...within the exact same code and see if you get an error?