Re: writing array to file?



On Aug 21, 12:40 pm, elroyerni <davechu...@xxxxxxxxx> wrote:
I have a array that i initialize for using this in my perl script:
my @array_data = qx(cat /home/dchun/logs/$date/stat.log|grep "System
took"|cut -c 56-62);

Okay... if you wanted to just do that in Perl rather than shelling out
to three system commands, it would be:

open my $fh, '<', "/home/dchun/logs/$date/stat.log" or die $!;
my @array_data;
while (my $line = <$fh>) {
if ($line =~ /System took/) {
push @array_data, substr($line, 55, 6);
}
}



I know this is really simple but I can't seem to figure it out.
How do i write this array to a text file in my home directory? I
just want to save it for later. If the file already exists, i
just want to write over it.

Open the file for writing, print the array to the file.

open my $ofh, '>', 'file_to_write.txt' or die $!;
for my $line (@array_data) {
print $ofh "$line\n";
}


I tried using a system command..

You seem unusually dependent on shelling out to external programs.
Perl is more than a shell scripting language. You should take some
time to explore the rest of its features. I suggest you read:
perldoc -f open
perldoc perlopentut
perldoc -f print


but that didn't work. Any suggestions
on a simple way to do this?

system `cat /home/ssi9gwy/logs/$date/rco.log|grep "OTS took"|cut -c
56-62 > /u/dchun/rco_stats/$date.rco_stat.log`;

This says to run the command between ``, and then attempt to execute
that command's *output*. backticks execute a command and return the
output. system() executes a command and returns the return value.


Paul Lalli

.



Relevant Pages

  • Re: Wait for background processes to complete
    ... To be able to execute commands in the background and wait for their ... The documentation I am referring to is http://perldoc.perl.org/. ... You can run a command in the background with: ... There is a general problem with perl documentation: ...
    (comp.lang.perl.misc)
  • Re: Perl For Amateur Computer Programmers
    ... >professional computer programmers could use with the same ease as Basic. ... >Perl For Amateur Computer Programmers ... Also, taking into account that you're appealing to "scientists", it ... Also, as a side note, you seem to use the noun "command" in a naive ...
    (comp.lang.perl.misc)
  • Obtaining complete Unix command line that evoked script as string
    ... If there is a more appropriate list for this, let me know; the other perl lists I've seen seem to specialised for this. ... Note this is not just the arguments of the call to the script, but everything including pipes and redirects, etc., e.g. ... Ideally the perl interpreter would grab the complete command line as its evoked and I'd access this via a variable. ...
    (perl.beginners)
  • Thanks for the comments Jan. 25, 2006
    ... "shadow" operating system which is running behind Windows. ... Perl can then collect data from that program and do ... that by sending longer strings etc. to the Windows clipboard and then having ... SendKeysuse a Ctrl V command to paste the information to the text editor ...
    (comp.lang.perl.misc)
  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)