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. Advertisements

  2. jesse

    Paul Lalli Guest

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

    chomp;
    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. Advertisements

  3. jesse

    marora Guest

    chomp($failure); # should do the trick
     
    marora, Feb 21, 2007
    #3
  4. jesse

    kens Guest

    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

    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

    Quite so. Thanks for pointing that out.

    Paul Lalli
     
    Paul Lalli, Feb 22, 2007
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.