writing array to file?

Discussion in 'Perl Misc' started by elroyerni, Aug 21, 2007.

  1. elroyerni

    elroyerni Guest

    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`;
     
    elroyerni, Aug 21, 2007
    #1
    1. Advertising

  2. elroyerni

    Paul Lalli Guest

    On Aug 21, 12:40 pm, elroyerni <> 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
     
    Paul Lalli, Aug 21, 2007
    #2
    1. Advertising

  3. elroyerni

    elroyerni Guest

    On Aug 21, 12:02 pm, Jim Gibson <> wrote:
    > In article <>,
    >
    >
    >
    > elroyerni <> 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


    Thanks everyone for your help. As you can see I'm new to perl and am
    very thankful for all your help and feedback as I try to master this
    language.
     
    elroyerni, Aug 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,437
    HNguyen
    Dec 21, 2004
  2. justin
    Replies:
    0
    Views:
    394
    justin
    May 17, 2004
  3. Jens Mittag
    Replies:
    4
    Views:
    620
    Dave Thompson
    Jun 17, 2004
  4. Replies:
    1
    Views:
    358
    Jeremy Sanders
    Sep 28, 2006
  5. Speed

    Writing BOOLEAN Array to a file

    Speed, Feb 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    310
    Ben Pfaff
    Feb 17, 2006
Loading...

Share This Page