hi
i want to write all the elements of an array to a file to generate a report
i tried the following code.
foreach(@main_time)
{
system([EMAIL=""]"echo \"@{$_}\[/EMAIL]" 1>> $file_name 2>&1");
}
But the its not writing into the file.

please suggest me how to do this

Recommended Answers

All 9 Replies

hi
i want to write all the elements of an array to a file to generate a report
i tried the following code.
foreach(@main_time)
{
system([EMAIL=""]"echo \"@{$_}\[/EMAIL]" 1>> $file_name 2>&1");
}
But the its not writing into the file.

please suggest me how to do this

Why are you using 'system' to write to a file? Have you not heard of Perl's file managing functions? Have you read any documentation about this?

no.,can u please suggest any docs

no.,can u please suggest any docs

Yes. The ones that come with your Perl distribution.

What OS are you running this on?

no.,can u please suggest any docs

http://perldoc.perl.org/functions/open.html

for example, your code should be like the following:

foreach ( @main_time )
{
    open FH, ">>$file_name" or die "can't open '$file_name': $!";
    
    print FH $_;

    close FH;
}

http://perldoc.perl.org/functions/open.html

for example, your code should be like the following:

foreach ( @main_time )
{
    open FH, ">>$file_name" or die "can't open '$file_name': $!";
    
    print FH $_;

    close FH;
}

What you mean, I'm sure is

open FH, ">>$file_name" or die "can't open '$file_name': $!"; # <<<< outside the loop
foreach ( @main_time )
{
    
    print FH $_;

}
close FH; # <<<<<<<< outside the loop

And nowadays, the '3-argument' with lexical file handle syntax is suggested:

open my $FH,'>>',$file_name' or die "Can't open $file_name: $!";

i'm sorry :) i wrote it at deep night :) i thought that the filename is different every iteration of the loop.

i thought that the filename is different every iteration of the loop.

Actually, that was the impression I had too. So you might be correct. Maybe the OP will clarify.

Actually, that was the impression I had too. So you might be correct. Maybe the OP will clarify.

According to the original post:

i want to write all the elements of an array to a file to generate a report

My impression could be wrong, in which case you are right trudge. Hopefully the OP will clarify.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.