On Feb 21, 11:50 am, "Paul Lalli" <mri...@gmail.com> wrote:
> On Feb 21, 11:08 am, "jesse" <jesse_ha...@premierinc.com> 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
|