Re: writing array to file?



In article <1187714457.955337.306830@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
elroyerni <davechunny@xxxxxxxxx> wrote:

Hi -

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);

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.

I tried using a system command.. 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`;


Perl has input and output facilities for reading and writing files,
regular expressions for searching for patterns in strings, and
functions for manipulating strings. You should consider using them
(untested):

#!/usr/local/bin/perl
#
use warnings;
use strict;
my $date = '???';
my $infile = "/home/ssi9gwy/logs/$date/rco.log";
my $outfile = "/u/dchun/rco_stats/$date.rco_stat.log";
open(my $in, '<', $infile) or die("Can't open $infile: $!");
open(my $out, '>', $outfile) or die("Can't open $outfile: $!");
while(<$in>) {
next unless /OTS took/;
print $out substr($_,56,7), "\n";
}
close($in) or die("Error closing $infile: $!");
close($out) or die("Error closing $outfile: $!");
__END__

See
perldoc -f open
perldoc -f substr
perldoc perlre
perldoc perlop (search for 'I/O Operators')

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • Re: style of functions with complex arguments
    ... > where the list at the end is actually a set of 3 triples. ... triples I would use an array reference as well. ... then you need to check that the 'weights' is an array ref (perldoc -f ...
    (perl.beginners)
  • How to grep $string from a file? How to run sed-like commands from Perl?
    ... Please note the Perl script that am trying to write at end the of this email, ... time and do a grep against the array. ... but it still consumes a lot of resource. ... convert this script to grep the string from a file instead of from the array. ...
    (perl.beginners)
  • RE: Why doesnt this work?
    ... Any time you don't understand a Perl function, look it up via perldoc. ... the shiftin the sub: ... Removes the first element of an array and stores it in $search. ...
    (perl.beginners)
  • Re: Easy Field Grabbing Question
    ... > ok another stupid question time! ... > how does the above work for an array?? ... While, with perldoc, it can sometimes be a little hard to figure out how ... and learn some of the basics first. ...
    (comp.lang.perl.misc)
  • RE: How to store the out put in StringBuffer
    ... I have been mentioning @tail. ... An array is the one variable that can do what you ... perldoc perlintro ... How to store the out put in StringBuffer ...
    (perl.beginners)