Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newlines and deprecations

Reply
Thread Tools

Newlines and deprecations

 
 
jesse
Guest
Posts: n/a
 
      02-21-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-21-2007
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

 
Reply With Quote
 
 
 
 
marora@gmail.com
Guest
Posts: n/a
 
      02-21-2007
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>) {
> 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



 
Reply With Quote
 
kens
Guest
Posts: n/a
 
      02-21-2007
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

 
Reply With Quote
 
jesse
Guest
Posts: n/a
 
      02-22-2007
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- 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.

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      02-22-2007
On Feb 21, 12:55 pm, "kens" <kenslate...@hotmail.com> wrote:
> On Feb 21, 11:50 am, "Paul Lalli" <mri...@gmail.com> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
two types of newlines (\n and \r\n) and browser HopfZ Javascript 1 01-11-2007 08:27 PM
newlines and sax.saxutils.quoteattr Edward K. Ream Python 2 09-19-2006 05:09 PM
strip newlines and blanks micklee74@hotmail.com Python 5 05-02-2006 11:54 AM
jython/wlst: How to avoid newlines in output from "cd" command (and others) davidmichaelkarr@gmail.com Python 0 12-23-2005 07:25 PM
Validating Newlines and Carriage Returns via Schema Porthos XML 1 07-26-2005 11:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57