Newlines and deprecations

Discussion in 'Perl Misc' started by jesse, Feb 21, 2007.

  1. jesse

    jesse Guest

    I have a perl script that parses a file of backup failures. I have it
    print what failed and how many times it's failed. The log file lines
    look like this:

    c3devweb3 C:\\
    c3devweb3 D:\\

    This is the name of the server and what failed. The scripts seems to
    be working okay but there are some problems in the output. Here is
    the script.

    #!/usr/bin/perl
    #
    use:strict;
    use warnings;
    my $file = "/export/home/jhardy/failed";
    my %failures;

    open(FAILED, "<", $file) or die "$!\n";
    while (<FAILED>) {
    s/#.*//;
    next if /^(\s)*$/;
    my $failure = ( split,(/ /, $_) );
    $failures{$failure}++;
    }
    close(FAILED);
    foreach $failure ( keys %failures) {
    print "The backup of $failure has failed $failures{$failure}
    times this month\n";
    }

    The output I get is this:

    The backup of c3duoraint1-bkup /
    has failed 2 times this month
    The backup of c3duoraint1-bkup /export/home
    has failed 2 times this month

    The output needs to be on one line ie;
    The backup of c3duoraint1-bkup / has failed 2 times this month.

    Can someone tell me what I'm doing wrong. I am also getting an error
    on the split it says the way I'm using it is deprecated and I'm not
    sure what I'm doing wrong there either.

    Thanks,
    Jesse
     
    jesse, Feb 21, 2007
    #1
    1. Advertising

  2. jesse

    Paul Lalli Guest

    On Feb 21, 11:08 am, "jesse" <> wrote:
    > I have a perl script that parses a file of backup failures. I have it
    > print what failed and how many times it's failed. The log file lines
    > look like this:
    >
    > c3devweb3 C:\\
    > c3devweb3 D:\\
    >
    > This is the name of the server and what failed. The scripts seems to
    > be working okay but there are some problems in the output. Here is
    > the script.
    >
    > #!/usr/bin/perl
    > #
    > use:strict;
    > use warnings;
    > my $file = "/export/home/jhardy/failed";
    > my %failures;
    >
    > open(FAILED, "<", $file) or die "$!\n";
    > while (<FAILED>) {


    You forgot to chomp. That's why you have a newline in your output.

    chomp;

    > s/#.*//;
    > next if /^(\s)*$/;
    > my $failure = ( split,(/ /, $_) );


    You have two different errors here that are working in conjunction to
    produce almost-correct results. For one, split should not have a
    comma before its first argument. For two, it's using split in a
    depreciated context that's depreciated. Fortunately, because you
    erroneously put that comma there, you're not getting the return value
    of split called in a scalar context stored into $failure. Instead,
    you're getting $_ itself - the whole line. Now because you're not
    complaining that you're getting the whole line of the file rather than
    just the part after the spaces, I can only assume that you really have
    no need to split at all. So just assign $failure to $_:
    my $failure = $_;

    If you really did want to split the line and use just the last part of
    it, then call split in a list context, and get the last element of the
    returned list:

    my $failure = (split / /, $_)[0];


    Paul Lalli
     
    Paul Lalli, Feb 21, 2007
    #2
    1. Advertising

  3. jesse

    Guest

    On Feb 21, 11:08 am, "jesse" <> wrote:
    > I have a perl script that parses a file of backup failures. I have it
    > print what failed and how many times it's failed. The log file lines
    > look like this:
    >
    > c3devweb3 C:\\
    > c3devweb3 D:\\
    >
    > This is the name of the server and what failed. The scripts seems to
    > be working okay but there are some problems in the output. Here is
    > the script.
    >
    > #!/usr/bin/perl
    > #
    > use:strict;
    > use warnings;
    > my $file = "/export/home/jhardy/failed";
    > my %failures;
    >
    > open(FAILED, "<", $file) or die "$!\n";
    > while (<FAILED>) {
    > s/#.*//;
    > next if /^(\s)*$/;
    > my $failure = ( split,(/ /, $_) );
    > $failures{$failure}++;}
    >
    > close(FAILED);
    > foreach $failure ( keys %failures) {

    chomp($failure); # should do the trick
    > print "The backup of $failure has failed $failures{$failure}
    > times this month\n";
    >
    > }
    >
    > The output I get is this:
    >
    > The backup of c3duoraint1-bkup /
    > has failed 2 times this month
    > The backup of c3duoraint1-bkup /export/home
    > has failed 2 times this month
    >
    > The output needs to be on one line ie;
    > The backup of c3duoraint1-bkup / has failed 2 times this month.
    >
    > Can someone tell me what I'm doing wrong. I am also getting an error
    > on the split it says the way I'm using it is deprecated and I'm not
    > sure what I'm doing wrong there either.
    >
    > Thanks,
    > Jesse
     
    , Feb 21, 2007
    #3
  4. jesse

    kens Guest

    On Feb 21, 11:50 am, "Paul Lalli" <> wrote:
    > On Feb 21, 11:08 am, "jesse" <> wrote:
    >
    >
    >
    > > I have a perl script that parses a file of backup failures. I have it
    > > print what failed and how many times it's failed. The log file lines
    > > look like this:

    >
    > > c3devweb3 C:\\
    > > c3devweb3 D:\\

    >
    > > This is the name of the server and what failed. The scripts seems to
    > > be working okay but there are some problems in the output. Here is
    > > the script.

    >
    > > #!/usr/bin/perl
    > > #
    > > use:strict;
    > > use warnings;
    > > my $file = "/export/home/jhardy/failed";
    > > my %failures;

    >
    > > open(FAILED, "<", $file) or die "$!\n";
    > > while (<FAILED>) {

    >
    > You forgot to chomp. That's why you have a newline in your output.
    >
    > chomp;
    >
    > > s/#.*//;
    > > next if /^(\s)*$/;
    > > my $failure = ( split,(/ /, $_) );

    >
    > You have two different errors here that are working in conjunction to
    > produce almost-correct results. For one, split should not have a
    > comma before its first argument. For two, it's using split in a
    > depreciated context that's depreciated. Fortunately, because you
    > erroneously put that comma there, you're not getting the return value
    > of split called in a scalar context stored into $failure. Instead,
    > you're getting $_ itself - the whole line. Now because you're not
    > complaining that you're getting the whole line of the file rather than
    > just the part after the spaces, I can only assume that you really have
    > no need to split at all. So just assign $failure to $_:
    > my $failure = $_;
    >
    > If you really did want to split the line and use just the last part of
    > it, then call split in a list context, and get the last element of the
    > returned list:
    >
    > my $failure = (split / /, $_)[0];
    >
    > Paul Lalli


    Looks like a typo - that will get the first element.
    The following would get the last:

    my $failure = (split / /, $_)[-1];

    Ken
     
    kens, Feb 21, 2007
    #4
  5. jesse

    jesse Guest

    On Feb 21, 11:50 am, "Paul Lalli" <> wrote:
    > On Feb 21, 11:08 am, "jesse" <> wrote:
    >
    >
    >
    >
    >
    > > I have a perl script that parses a file of backup failures. I have it
    > > print what failed and how many times it's failed. The log file lines
    > > look like this:

    >
    > > c3devweb3 C:\\
    > > c3devweb3 D:\\

    >
    > > This is the name of the server and what failed. The scripts seems to
    > > be working okay but there are some problems in the output. Here is
    > > the script.

    >
    > > #!/usr/bin/perl
    > > #
    > > use:strict;
    > > use warnings;
    > > my $file = "/export/home/jhardy/failed";
    > > my %failures;

    >
    > > open(FAILED, "<", $file) or die "$!\n";
    > > while (<FAILED>) {

    >
    > You forgot to chomp. That's why you have a newline in your output.
    >
    > chomp;
    >
    > > s/#.*//;
    > > next if /^(\s)*$/;
    > > my $failure = ( split,(/ /, $_) );

    >
    > You have two different errors here that are working in conjunction to
    > produce almost-correct results. For one, split should not have a
    > comma before its first argument. For two, it's using split in a
    > depreciated context that's depreciated. Fortunately, because you
    > erroneously put that comma there, you're not getting the return value
    > of split called in a scalar context stored into $failure. Instead,
    > you're getting $_ itself - the whole line. Now because you're not
    > complaining that you're getting the whole line of the file rather than
    > just the part after the spaces, I can only assume that you really have
    > no need to split at all. So just assign $failure to $_:
    > my $failure = $_;
    >
    > If you really did want to split the line and use just the last part of
    > it, then call split in a list context, and get the last element of the
    > returned list:
    >
    > my $failure = (split / /, $_)[0];
    >
    > Paul Lalli- Hide quoted text -
    >
    > - Show quoted text -


    I have a major error in my logic and wanted to know if you could give
    me some pointers. I need to count the number of occurences for each
    failure in the file but if the failure didn't happen the day of the
    report I want to ignore any of those failures in the master file until
    the failure happens again. I don't think I can really use a regular
    expression because I'm not looking for a specific word. I have 400
    clients that I backup on a nightly basis and it could be any of those
    400.
     
    jesse, Feb 22, 2007
    #5
  6. jesse

    Paul Lalli Guest

    On Feb 21, 12:55 pm, "kens" <> wrote:
    > On Feb 21, 11:50 am, "Paul Lalli" <> wrote:
    > > If you really did want to split the line and use just the last
    > > part of it, then call split in a list context, and get the last
    > > element of the returned list:

    >
    > > my $failure = (split / /, $_)[0];


    > Looks like a typo - that will get the first element.
    > The following would get the last:
    >
    > my $failure = (split / /, $_)[-1];


    Quite so. Thanks for pointing that out.

    Paul Lalli
     
    Paul Lalli, Feb 22, 2007
    #6
    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. Porthos
    Replies:
    1
    Views:
    587
    C. M. Sperberg-McQueen
    Jul 27, 2005
  2. Replies:
    0
    Views:
    493
  3. Replies:
    5
    Views:
    487
    Kent Johnson
    May 2, 2006
  4. Edward K. Ream

    newlines and sax.saxutils.quoteattr

    Edward K. Ream, Sep 19, 2006, in forum: Python
    Replies:
    2
    Views:
    340
    Edward K. Ream
    Sep 19, 2006
  5. HopfZ
    Replies:
    1
    Views:
    113
    John G Harris
    Jan 11, 2007
Loading...

Share This Page