Re: writing array to file?
- From: Paul Lalli <mritty@xxxxxxxxx>
- Date: Tue, 21 Aug 2007 09:48:32 -0700
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
.
- References:
- writing array to file?
- From: elroyerni
- writing array to file?
- Prev by Date: Re: how to call sub by value in variable
- Next by Date: Re: Perl and JSP
- Previous by thread: writing array to file?
- Next by thread: Re: writing array to file?
- Index(es):
Relevant Pages
|