Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

printing of an array

by manishrathi (Beadle)
on Mar 10, 2013 at 23:10 UTC ( #1022709=perlquestion: print w/ replies, xml ) Need Help??
manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

$filelist = "$home/filelist-${wfid}.txt"; my @files = $task->GetFiles(); open FILELIST, ">$filelist"; foreach my $file (@files){ print FILELIST "$file\n"; } close FILELIST;
In this syntax, each member of @files is read in $file, what happens after that ?
What does following line of code do ?
print FILELIST "$file\n" ;

Comment on printing of an array
Select or Download Code
Re: printing of an array
by kcott (Abbot) on Mar 10, 2013 at 23:22 UTC

    G'day manishrathi,

    I can only imagine your question contains a typo or some omission!

    What the print function does is very basic and I can't imagine you haven't encountered it before.

    In any event, why don't you just run the code and see for yourself what print FILELIST "$file\n"; does.

    -- Ken

Re^2: printing of an array
by perl-diddler (Hermit) on Mar 11, 2013 at 00:24 UTC
    check out the man page called "perlfunc", it describes all of perl's functions and what they do. if you are running on a shell that lets you create aliases or functions (like bash), a good alias is:
    alias perlfunc='perldoc -f' # or as a function: function perlfunc { local func=${1:?"Need a function name"} perldoc "$1" } # add one of the above to your ".bashrc...
    Then you can just type the name of the function:
    > perlfunc print KFPMKBZGPT(1) User Contributed Perl Documentation KFPMK +BZGPT(1) print FILEHANDLE LIST print FILEHANDLE print LIST print Prints a string or a list of strings. Returns true if successful. .... Printing to a closed pipe or socket will generate a SIG +PIPE signal. See perlipc for more on signal handling. perl v5.16.2 2013-03-10 KFPMK +BZGPT(1)
    Note, the above is not the whole entry (it's a page long, but will tell you exactly what it does...

    if you have your documentation installed as 'manpages',

    man perlfunc
    will show you the whole list. (it's *way* long...)

    Hope that helps...

      $filelist = "C:\Users\cign\Desktop\printFile.txt"; my @files = (file1, file2, file3); open(WRITEFILE, ">$filelist") || die("ERROR: unable to open file \n") +; select WRITEFILE; foreach my $file (@files){ print WRITEFILE ; #"$file\n"; } close WRITEFILE;
      I tried running above code, but I am getting error message unable to open file. WHats wrong in here, why can this file handle not open ?
        WHats wrong in here, why can this file handle not open ?

        It's good that you are checking the success of the open function call and calling die if it fails. The answer to the question "why?" is contained in the  $! error variable (see perlvar, especially the discussion in Error Variables). Put that in your die function and see the result:
            open(WRITEFILE, ">$filelist") || die("ERROR: unable to open file: $! \n");

        In addition, the statement
            my @files = (file1, file2, file3);
        suggests you are not using warnings and strict at the beginning of your code:
            use warnings;
            use strict;
        I strongly suggest you do so.

        Furthermore, the statement
            $filelist = "C:\Users\cign\Desktop\printFile.txt";
        is highly suspect. Add a print statement for  $filelist immediately after this assignment and check if what is printed is what you expect. The other tactic is to include the filename in delimiters in the die call discussed above:
            open(WRITEFILE, ">$filelist") || die("opening '$filelist': $!\n");

        The next step is to use the three-argument form of open:
            open my $writefile, '>', $filelist or die "opening '$filelist': $!"

        What's wrong is the way you are entering the file name. Double quotes allow interpretation of what is between them:
        I.e. step 1:
        print out the name of the file:
        perl -e ' use P; my $filelist="C:\Users\cign\Desktop\printFile.txt"; P "opening file: %s", $filelist; ' opening file: C:SERS GNDESKTOPPRINTFILE.TXT
        Is that the file you wanted to open? Prolly not.
        step 2, at least turn on warning and try to use strict - they detect alot (though not all) of problems -- in this case they would have given you a hint:
        perl -we 'use strict; use P; my $filelist="C:\Users\cign\Desktop\printFile.txt"; P "opening file: %s", $filelist;' Unrecognized escape \D passed through at -e line 2. Unrecognized escape \p passed through at -e line 2. opening file: C:SERS GNDESKTOPPRINTFILE.TXT

        use single quotes to define a literal, or put another backslash \ before each \, or use the 'q{...}' operator to really set it off that emphasizes it has to be taken literally (and doesn't interfere with my using single quotes in a 1 line program I typed in on the command line! ;-)

        perl -we 'use strict; use P; my $filelist=q{C:\Users\cign\Desktop\printFile.txt}; P "opening file: %s", $filelist;' opening file: C:\Users\cign\Desktop\printFile.txt ## #alternate ways ## > perl -we 'use strict; use P; my $filelist='\''C:\Users\cign\Desktop\printFile.txt'\''; P "opening file: %s", $filelist;' opening file: C:\Users\cign\Desktop\printFile.txt ## > perl -we 'use strict; use P; my $filelist="C:\\Users\\cign\\Desktop\\printFile.txt"; P "opening file: %s", $filelist;' opening file: C:\Users\cign\Desktop\printFile.txt ### ## my favorite (in a file)... #! perl/bin/perl.exe use strict; use P; my $filelist="C:/Users/../tmp/../Perl64/printFile.txt"; P "opening file: %s", $filelist; open (test, ">", $filelist) or die "didn't work"; print test "Hello World!\n"; close test; C:\Perl64>perl test.pl opening file: C:/Users/../tmp/../Perl64/printFile.txt C:\Perl64>type printFile.txt Hello World!

        Forward slash works in alot of places on Windows cuz the underlying 'NT' OS accepts it.

Re: printing of an array
by Kenosis (Priest) on Mar 11, 2013 at 01:28 UTC

    toolic provided an explanation of FILELIST in that code segment in your posting where you asked, "Is file handler FILELIST adding members to $filelist ?" Was there something in toolic's explanation that needed clarification?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022709]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-07-13 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (248 votes), past polls