User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 423,616 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,220 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 2523 | Replies: 9
Reply
Join Date: Apr 2007
Posts: 14
Reputation: pkraop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
pkraop's Avatar
pkraop pkraop is offline Offline
Newbie Poster

Writing array elements to a file in perl

  #1  
Oct 15th, 2007
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("echo \"@{$_}\" 1>> $file_name 2>&1");
}
But the its not writing into the file.

please suggest me how to do this
Rao
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Writing array elements to a file in perl

  #2  
Oct 15th, 2007
Originally Posted by pkraop View Post
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("echo \"@{$_}\" 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?
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Apr 2007
Posts: 14
Reputation: pkraop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
pkraop's Avatar
pkraop pkraop is offline Offline
Newbie Poster

Re: Writing array elements to a file in perl

  #3  
Oct 16th, 2007
no.,can u please suggest any docs
Rao
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Writing array elements to a file in perl

  #4  
Oct 16th, 2007
Originally Posted by pkraop View Post
no.,can u please suggest any docs


Yes. The ones that come with your Perl distribution.

What OS are you running this on?
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Oct 2007
Location: Russia, St-Petersburg
Posts: 8
Reputation: cerf_machine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cerf_machine's Avatar
cerf_machine cerf_machine is offline Offline
Newbie Poster

Re: Writing array elements to a file in perl

  #5  
Oct 16th, 2007
Originally Posted by pkraop View Post
no.,can u please suggest any docs


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

for example, your code should be like the following:

  1.  
  2. foreach ( @main_time )
  3. {
  4. open FH, ">>$file_name" or die "can't open '$file_name': $!";
  5.  
  6. print FH $_;
  7.  
  8. close FH;
  9. }
  10.  
Last edited by cerf_machine : Oct 16th, 2007 at 7:36 pm.
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Writing array elements to a file in perl

  #6  
Oct 16th, 2007
Originally Posted by cerf_machine View Post
http://perldoc.perl.org/functions/open.html

for example, your code should be like the following:

  1.  
  2. foreach ( @main_time )
  3. {
  4. open FH, ">>$file_name" or die "can't open '$file_name': $!";
  5.  
  6. print FH $_;
  7.  
  8. close FH;
  9. }
  10.  


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: $!";
Last edited by trudge : Oct 16th, 2007 at 8:27 pm.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Oct 2007
Location: Russia, St-Petersburg
Posts: 8
Reputation: cerf_machine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cerf_machine's Avatar
cerf_machine cerf_machine is offline Offline
Newbie Poster

Re: Writing array elements to a file in perl

  #7  
Oct 17th, 2007
i'm sorry i wrote it at deep night i thought that the filename is different every iteration of the loop.
Reply With Quote  
Join Date: Mar 2006
Posts: 618
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: Writing array elements to a file in perl

  #8  
Oct 17th, 2007
Originally Posted by cerf_machine View Post
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.
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Writing array elements to a file in perl

  #9  
Oct 17th, 2007
Originally Posted by KevinADC View Post
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
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Mar 2006
Posts: 618
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: Writing array elements to a file in perl

  #10  
Oct 17th, 2007
My impression could be wrong, in which case you are right trudge. Hopefully the OP will clarify.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 9:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC