Re: Newline error

2006-06-06 Thread Xavier Noria

On Jun 6, 2006, at 12:59, joseph wrote:

All,

Just like to ask for correction on what's wrong with my script it  
gives spit

out this error when i run it.

Unsuccessful open on filename containing newline at disksize.pl  
line 8.

Can't open file No such file or directory

But it runs without this error whenever i feed it up when an  
existing file

output by df-h.
ex:## open(FL,path/toactual/file) or die blabalha;
Does this mean it can't trap the output of  `df-h`?


Dealing with system commands (ls, ps, df, etc.) that way is usually  
much tricky than it looks. Normally one goes to CPAN and checks  
whether someone has done it right already. In that case one installs  
the module and writes an easy and robust script in 5 minutes.


For this particular problem delegate to Sys::Filesystem.

-- fxn




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread John W. Krahn
joseph wrote:
 All,

Hello,

 Just like to ask for correction on what's wrong with my script it gives spit 
 out this error when i run it.
 
 Unsuccessful open on filename containing newline at disksize.pl line 8.
 Can't open file No such file or directory
 
 But it runs without this error whenever i feed it up when an existing file 
 output by df-h.
 ex:## open(FL,path/toactual/file) or die blabalha;
 Does this mean it can't trap the output of  `df-h`?
 
 Here's the script###
 
 #!/usr/bin/perl
 
 use strict;
 use warnings;
 
 chomp(my $output_file = `df -h`);

`df -h` does not return a file name, it returns the same output as if you had
run the command 'df -h' on the command line.


 open(FL,$output_file) or die Can't open file $!\n;
 my @list;
 my %disk;
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
 while(FL) {

There are two basic ways to do what you want:

1.  for ( `df -h` ) { ... }

2.  open FL, 'df -h |' or die Cannot open df pipe: $!;
while ( FL ) { ... }
close FL or warn $! ? Error closing df pipe: $!
: Exit status $? from df;


  ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);

You are only using two of the values so you can simplify that to:

   my ( $percent, $mounted ) = ( split )[ -2, -1 ];


  push @list,$mounted,$percent;

You don't need the array, you can assign directly to the hash:

   $disk{ $mounted } = $percent unless $mounted eq 'Mounted';


 }
 
 %disk =(@list);
 delete $disk{Mounted};



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread Mr. Shawn H. Corey
On Tue, 2006-06-06 at 18:59 +0800, joseph wrote:
 All,
 
 Just like to ask for correction on what's wrong with my script it gives spit 
 out this error when i run it.
 
 Unsuccessful open on filename containing newline at disksize.pl line 8.
 Can't open file No such file or directory
 
 But it runs without this error whenever i feed it up when an existing file 
 output by df-h.
 ex:## open(FL,path/toactual/file) or die blabalha;
 Does this mean it can't trap the output of  `df-h`?
 
 Here's the script###
 
 #!/usr/bin/perl
 
 use strict;
 use warnings;
 
 chomp(my $output_file = `df -h`);

$output_file =~ s/\r//g;
# chomp only removes linefeed characters \n

# BTW, there is no such thing as a newline;
# it is either a linefeed: \n ASCII LF 0x0A
# or a carriage return: \r ASCII CR 0x0D

 
 open(FL,$output_file) or die Can't open file $!\n;

... or die cannot open file '$output_file': $!\n;
# print out the contents so you can see exactly what it contains.

 my @list;
 my %disk;
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
 while(FL) {
  ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
  push @list,$mounted,$percent;
 }
 
 %disk =(@list);
 delete $disk{Mounted};
 
 
 
 foreach (sort keys %disk) {
my $value = $disk{$_};
print \t $_ = $value \n;
 }



-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

For the things we have to learn before we can do them, we learn by doing them.
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread Muma W.

joseph wrote:

All,

Just like to ask for correction on what's wrong with my script it gives spit 
out this error when i run it.


Unsuccessful open on filename containing newline at disksize.pl line 8.
Can't open file No such file or directory

But it runs without this error whenever i feed it up when an existing file 
output by df-h.

ex:## open(FL,path/toactual/file) or die blabalha;
Does this mean it can't trap the output of  `df-h`?

Here's the script###

#!/usr/bin/perl

use strict;
use warnings;

chomp(my $output_file = `df -h`);



Df can return multiple lines. I'd suspect that your string $output_file
has several embedded newlines in it at this point. Chomp will only
remove that last (not embedded) newline.


open(FL,$output_file) or die Can't open file $!\n;
my @list;
my %disk;
my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
while(FL) {
 ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
 push @list,$mounted,$percent;


You might not need the intermediate list:
$disk{$mounted} = $percent;


}

%disk =(@list);
delete $disk{Mounted};



foreach (sort keys %disk) {
   my $value = $disk{$_};
   print \t $_ = $value \n;
}






I'd do it like this:

use strict;
use warnings;
use Data::Dumper;

my %disk;
for (`df -h`) {
next if ! m{/};
my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage) =
split /\s+/, $_;
$disk{$mounted} = $percent;
}
print Dumper(\%disk);

__END__

You'll notice that the backticks operator (``), in a list context,
returns a list containing each line as a separate list element.

Since you only seem to need two elements from the list returned by
split(), you could change the two lines after the m{/} to this:

my @list = split /\s+/, $_;
$disk{$list[5]} = $list[4];

HTH




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread John W. Krahn
Mr. Shawn H. Corey wrote:
 
 $output_file =~ s/\r//g;
 # chomp only removes linefeed characters \n
 
 # BTW, there is no such thing as a newline;
 # it is either a linefeed: \n ASCII LF 0x0A
 # or a carriage return: \r ASCII CR 0x0D

\n is inherited from the C programming language and is the newline escape
sequence.  On Unix and Unix-like systems the newline is equivalent to the
ASCII line feed character but on other systems it could be one or more
different characters.

http://en.wikipedia.org/wiki/Newline


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread Xavier Noria

On Jun 6, 2006, at 14:16, John W. Krahn wrote:


Mr. Shawn H. Corey wrote:


$output_file =~ s/\r//g;
# chomp only removes linefeed characters \n

# BTW, there is no such thing as a newline;
# it is either a linefeed: \n ASCII LF 0x0A
# or a carriage return: \r ASCII CR 0x0D


\n is inherited from the C programming language and is the  
newline escape
sequence.  On Unix and Unix-like systems the newline is equivalent  
to the

ASCII line feed character but on other systems it could be one or more
different characters.


To be more precise, \n in Perl is eq \012 everywhere except in  
Mac OS pre-X, where it is eq \015. In CRLF platforms like Win32  
\n is transparently converted to CRLF on writing and back on  
reading by PerlIO in text mode. Thus, in a regular line-oriented  
script like


  while (my $line = FH) {
  # work with $line
  }

$line ends with \n but does not contain a pair CRLF (assuming  
native conventions in the input). On the other direction, the string  
foo\n has length 4 in all systems. When you print that string into  
a file in text mode on Windows the bytes on disk have an extra  
\015, but that's transparent to the programmer. That's the point of  
using \n as logical/portable newline in Perl.


I have written an article about newlines in Perl not yet published.  
All those fine details are explained there.


-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread joseph

Mr. Shawn H. Corey [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Tue, 2006-06-06 at 18:59 +0800, joseph wrote:
 All,

 Just like to ask for correction on what's wrong with my script it gives 
 spit
 out this error when i run it.

 Unsuccessful open on filename containing newline at disksize.pl line 8.
 Can't open file No such file or directory

 But it runs without this error whenever i feed it up when an existing 
 file
 output by df-h.
 ex:## open(FL,path/toactual/file) or die blabalha;
 Does this mean it can't trap the output of  `df-h`?

 Here's the script###

 #!/usr/bin/perl

 use strict;
 use warnings;

 chomp(my $output_file = `df -h`);

 $output_file =~ s/\r//g;
 # chomp only removes linefeed characters \n

 # BTW, there is no such thing as a newline;
 # it is either a linefeed: \n ASCII LF 0x0A
 # or a carriage return: \r ASCII CR 0x0D

I still have to dig on this topic, i assume  it works the same way like the 
sample on a book like this # chomp($file = `date`);



 open(FL,$output_file) or die Can't open file $!\n;

 ... or die cannot open file '$output_file': $!\n;
 # print out the contents so you can see exactly what it contains.

Unsuccessful open on filename containing newline at disksize.pl line 8.
Can't open file: FilesystemSize  Used Avail Use% Mounted on
/dev/cciss/c0d0p7 1.9G  215M  1.6G  12% /
/dev/cciss/c0d0p8  12G  9.8G  1.4G  87% /backup
/dev/cciss/c0d0p1  96M   16M   76M  17% /boot
/dev/cciss/c0d0p2  38G   24G   12G  66% /home
none  1.9G 0  1.8G   0% /dev/shm No such file or 
directory

#It's present right?

Thanks for input.


 my @list;
 my %disk;
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
 while(FL) {
  ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
  push @list,$mounted,$percent;
 }

 %disk =(@list);
 delete $disk{Mounted};



 foreach (sort keys %disk) {
my $value = $disk{$_};
print \t $_ = $value \n;
 }



 -- 
 __END__

 Just my 0.0002 million dollars worth,
   --- Shawn

 For the things we have to learn before we can do them, we learn by doing 
 them.
  Aristotle

 * Perl tutorials at http://perlmonks.org/?node=Tutorials
 * A searchable perldoc is at http://perldoc.perl.org/

 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread joseph

John W. Krahn [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 joseph wrote:
 All,

 Hello,

 Just like to ask for correction on what's wrong with my script it gives 
 spit
 out this error when i run it.

 Unsuccessful open on filename containing newline at disksize.pl line 8.
 Can't open file No such file or directory

 But it runs without this error whenever i feed it up when an existing 
 file
 output by df-h.
 ex:## open(FL,path/toactual/file) or die blabalha;
 Does this mean it can't trap the output of  `df-h`?

 Here's the script###

 #!/usr/bin/perl

 use strict;
 use warnings;

 chomp(my $output_file = `df -h`);

 `df -h` does not return a file name, it returns the same output as if you 
 had
 run the command 'df -h' on the command line.


 open(FL,$output_file) or die Can't open file $!\n;
 my @list;
 my %disk;
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
 while(FL) {

 There are two basic ways to do what you want:

 1.  for ( `df -h` ) { ... }

 2.  open FL, 'df -h |' or die Cannot open df pipe: $!;
while ( FL ) { ... }
close FL or warn $! ? Error closing df pipe: $!
: Exit status $? from df;


  ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);

 You are only using two of the values so you can simplify that to:

   my ( $percent, $mounted ) = ( split )[ -2, -1 ];


  push @list,$mounted,$percent;

 You don't need the array, you can assign directly to the hash:

   $disk{ $mounted } = $percent unless $mounted eq 'Mounted';


 }

 %disk =(@list);
 delete $disk{Mounted};



 John
 -- 
 use Perl;
 program
 fulfillment

Noted, thanks.. for all the helpful feedback it makes my learning stage 
worthwhile. 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error (Closed)

2006-06-06 Thread joseph

Muma W. [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 joseph wrote:
 All,

 Just like to ask for correction on what's wrong with my script it gives 
 spit out this error when i run it.

 Unsuccessful open on filename containing newline at disksize.pl line 8.
 Can't open file No such file or directory

 But it runs without this error whenever i feed it up when an existing 
 file output by df-h.
 ex:## open(FL,path/toactual/file) or die blabalha;
 Does this mean it can't trap the output of  `df-h`?

 Here's the script###

 #!/usr/bin/perl

 use strict;
 use warnings;

 chomp(my $output_file = `df -h`);


 Df can return multiple lines. I'd suspect that your string $output_file
 has several embedded newlines in it at this point. Chomp will only
 remove that last (not embedded) newline.

 open(FL,$output_file) or die Can't open file $!\n;
 my @list;
 my %disk;
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
 while(FL) {
  ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
  push @list,$mounted,$percent;

 You might not need the intermediate list:
 $disk{$mounted} = $percent;

 }

 %disk =(@list);
 delete $disk{Mounted};



 foreach (sort keys %disk) {
my $value = $disk{$_};
print \t $_ = $value \n;
 }





 I'd do it like this:

 use strict;
 use warnings;
 use Data::Dumper;

 my %disk;
 for (`df -h`) {
 next if ! m{/};
 my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage) =
 split /\s+/, $_;
 $disk{$mounted} = $percent;
 }
 print Dumper(\%disk);

 __END__

 You'll notice that the backticks operator (``), in a list context,
 returns a list containing each line as a separate list element.

 Since you only seem to need two elements from the list returned by
 split(), you could change the two lines after the m{/} to this:

 my @list = split /\s+/, $_;
 $disk{$list[5]} = $list[4];

 HTH

Thank you, your way resemble with the first option John Krahn reply..i'm 
just late to close the thread due to timezone difference still i appreciate 
your effort.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newline error

2006-06-06 Thread joseph

Xavier Noria [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Jun 6, 2006, at 14:16, John W. Krahn wrote:

 Mr. Shawn H. Corey wrote:

 $output_file =~ s/\r//g;
 # chomp only removes linefeed characters \n

 # BTW, there is no such thing as a newline;
 # it is either a linefeed: \n ASCII LF 0x0A
 # or a carriage return: \r ASCII CR 0x0D

 \n is inherited from the C programming language and is the  newline 
 escape
 sequence.  On Unix and Unix-like systems the newline is equivalent  to 
 the
 ASCII line feed character but on other systems it could be one or more
 different characters.

 To be more precise, \n in Perl is eq \012 everywhere except in  Mac OS 
 pre-X, where it is eq \015. In CRLF platforms like Win32  \n is 
 transparently converted to CRLF on writing and back on  reading by PerlIO 
 in text mode. Thus, in a regular line-oriented  script like

   while (my $line = FH) {
   # work with $line
   }

 $line ends with \n but does not contain a pair CRLF (assuming  native 
 conventions in the input). On the other direction, the string  foo\n has 
 length 4 in all systems. When you print that string into  a file in text 
 mode on Windows the bytes on disk have an extra  \015, but that's 
 transparent to the programmer. That's the point of  using \n as 
 logical/portable newline in Perl.

 I have written an article about newlines in Perl not yet published.  All 
 those fine details are explained there.

 -- fxn

--expect me to read it..do give me a link once done, thanks. 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response