Re: newbie question : about the perl sprintf
On Wed, 21 Oct 2009 20:52:05 +0800, Majian wrote: And I modify it like this sprintf The number in scientific notation is %e, 01.255; The screen now output is The number in scientific notation is 1.255000e+03 Ha, this is an interesting case. By putting the zero before the 1, it turns it into an octal number and now the period becomes the concatenation operator instead of a decimal point, yielding a term of 1255. Try printf The number in scientific notation is %e, 037.255; and see what happens. -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ http://www.informit.com/store/product.aspx?isbn=0137001274 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Why Perl's representative is a camel?
I'm always wanting to know this. Thanks. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Why Perl's representative is a camel?
2009/10/24 net...@royal.net: I'm always wanting to know this. Thanks. O'Reilly media, publishers of Programming Perl (as well as Learning, Advance, Mastering, Best Practices, Cookbook and many other Perl books), have a practice of placing animal drawings on the cover of the books and the Programming Perl book have a drawing of a Camel on its cover. Being one of the dominant publication of tech books, if the book becomes canonical, or just famous, the animal on the cover tends to be associated with the technology the book is about. For instance, the Java implementation of JavaScript is called Rhino after the Rhino painted on the O'Reilly JavaScript book. The Camel is actually a trademark of O'Reilly, and the Perl Foundation is using an onion logo, rather than a camel. There's also a saying among Perl programmers that Perl is like a camel, ugly but efficient. The merits of this saying is debatable. Some more information here: http://oreilly.com/pub/a/oreilly/perl/usage/ -- Erez The government forgets that George Orwell's 1984 was a warning, and not a blueprint http://www.nonviolent-conflict.org/ -- http://www.whyweprotest.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Why Perl's representative is a camel?
Erez Schatz wrote: The Camel is actually a trademark of O'Reilly, and the Perl Foundation is using an onion logo, rather than a camel. There's also a saying among Perl programmers that Perl is like a camel, ugly but efficient. The merits of this saying is debatable. SHREK: strikeOgres are/strike Perl is like onions. DONKEY: [Sniffs] They stink? SHREK: Yes. No! DONKEY: They make you cry? SHREK: No! DONKEY: You leave them out in the sun, they get all brown, start sprouting' little white hairs. SHREK: No! Layers! Onions have layers! -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
More on perl like tail
Sorry about being tricky with what was an older thread. But I suspect that thread died... and no one noticed there was an unaswered question still there. Shawn C originally suggested I use File::Tail. There was a short exchange about why and then I began trying to use File::Tail but haven't been successful with it. The details are below: Shawn H Corey shawnhco...@gmail.com writes: open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!; while(FILE){ print; if(eof){ sleep 2; seek (FILE,0,1); } } It seems at least to survive repeated restarts of system logger. If I write my script based on this code... what I'd be adding would be code to get 1 or 2 rgx from the cmdline, then write the hits to various files. [...] In the general case, modules usually take care of special cases that you may not be aware of. That makes them the preferred method of solving a problem. In this case, the above code is a kludge. You can tell this because the program sleeps, rather than waiting on input. When a program does something to emulate what it really should be doing, it introduces code that may not work in all cases. I'm having trouble with File::Tail... or more likely the way I'm trying to use it is wrongly setup. But first about that sleep comment. As I read a little of File::Tail and its very likely I'm not really understanding what I'm reading but, it appears to be saying that it `sleeps' at times... the times are a little more sophisticated... but none the less sleep. Now the problem. I've taken the first examples in the perldoc File::Tail output: (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm) use File::Tail; $file=File::Tail-new(/some/log/file); while (defined($line=$file-read)) { print $line; } And tied to make it work for my case (/var/adm/slpipe in the script is a named-pipe that the system logger reads into): cat fltr_sl #!/usr/local/bin/perl use strict; use warnings; use File::Tail; my ($file,$line); my $fname_in = /var/adm/slpipe; $file=File::Tail-new($fname_in); while (defined($line=$file-read)) { print $line; } When I run it, it doesn't show any errors, and appears to be waiting on the pipe. But no data ever comes out. I used the same test sequence as for the earlier script that didn't use File::Tail (posted earlier in this thread). The sequence is. 1) start the script `fltr_sl' shown above 2) To make sure if data is flowing thru the pipe start start (in a different xterm) tail -f slpipe too. 2) kill -HUP the system logger. 3) Ensure some data is flowing thru the named-pipe by running ssh r...@localhost from a user account I see 7-8 lines output to the tail -f slpipe command, but nothing to the perl script fltr_sl. And as I write this message, quite a few more lines are appearing at the `tail -f slpipe' cmd, but still nothing at my perl filter. Have I made some serious mistake in my attempted usage of File::Tail? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
printf and zero padding
With this little script, how would I manage to get the shorter timestamps zero padded using printf? I now how to get padded numbers but not when I'm pushing off the right margin too. cat script.pl #!/usr/local/bin/perl use strict; use warnings; while (my $file = shift @ARGV){ my @stat = stat $file; printf %11d %s\n,$stat[9], $file; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf and zero padding
At 4:02 PM -0500 10/24/09, Harry Putnam wrote: With this little script, how would I manage to get the shorter timestamps zero padded using printf? I now how to get padded numbers but not when I'm pushing off the right margin too. cat script.pl #!/usr/local/bin/perl use strict; use warnings; while (my $file = shift @ARGV){ my @stat = stat $file; printf %11d %s\n,$stat[9], $file; } I am not clear on what you are asking. You would use the format conversion '%011d' to zero-pad a number to fill 11 characters, but you say you know that already. Can you give an example of your desired output? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: More on perl like tail
At 3:57 PM -0500 10/24/09, Harry Putnam wrote: Sorry about being tricky with what was an older thread. But I suspect that thread died... and no one noticed there was an unaswered question still there. Or no one knew the answer. Shawn C originally suggested I use File::Tail. There was a short exchange about why and then I began trying to use File::Tail but haven't been successful with it. The details are below: Shawn H Corey shawnhco...@gmail.com writes: open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!; while(FILE){ print; if(eof){ sleep 2; seek (FILE,0,1); } } I would modify the above a little. You are calling eof after each line, which is unnecessary. The input operator FILE will return undef whenever eof will return true, so calling eof is redundant. You can make this a little more efficient with something like the following (untested): while(1) { while(FILE) { print; } sleep 1; seek(FILE,0,1); } In this case, the above code is a kludge. You can tell this because the program sleeps, rather than waiting on input. When a program does something to emulate what it really should be doing, it introduces code that may not work in all cases. I'm having trouble with File::Tail... or more likely the way I'm trying to use it is wrongly setup. But first about that sleep comment. As I read a little of File::Tail and its very likely I'm not really understanding what I'm reading but, it appears to be saying that it `sleeps' at times... the times are a little more sophisticated... but none the less sleep. I do not agree with that kludge comment. When waiting for slow events, you can either use polling or interrupts. Polling means continually sleeping and then checking if the event has occurred. The event in this case is additional data appearing at the end of the file. Interrupt would mean blocking until the event has occurred. That can be accomplished using the Unix select statement. In the general case, interrupts are more efficient than polling, because your process does not execute at all until the event occurs. In this case, however, your process is waiting on another, slower process: the file writer. If you were to use a select statement, at the cost of increased complexity in your program, you would respond faster to new data. However, sleeping for 1 second and trying to read the file is not going to put a large load on your system. So the simple method using sleep will cost you less than one second every time you exhaust the file input in your reader process. If this is acceptable, then using sleep is OK. File::Tail does have a select method to improve response, but I think that in most cases that is overkill. As you have pointed out, the normal use of File::Tail involves calling the sleep function. Now the problem. I've taken the first examples in the perldoc File::Tail output: (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm) use File::Tail; $file=File::Tail-new(/some/log/file); while (defined($line=$file-read)) { print $line; } That looks OK, and works for me on /var/log/system.log, although the delays are longer using File::Tail than Unix tail. Maybe you should try it on a normal file that you write to occasionally with another Perl program. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: More on perl like tail
Jim Gibson jimsgib...@gmail.com writes: At 3:57 PM -0500 10/24/09, Harry Putnam wrote: Sorry about being tricky with what was an older thread. But I suspect that thread died... and no one noticed there was an unaswered question still there. Or no one knew the answer. He he... unlikely here I think.. [...] I would modify the above a little. You are calling eof after each line, which is unnecessary. The input operator FILE will return undef whenever eof will return true, so calling eof is redundant. You can make this a little more efficient with something like the following (untested): while(1) { while(FILE) { print; } sleep 1; seek(FILE,0,1); } That does look like it might be better... and thanks for the explanation. [...] use File::Tail; $file=File::Tail-new(/some/log/file); while (defined($line=$file-read)) { print $line; } That looks OK, and works for me on /var/log/system.log, although the delays are longer using File::Tail than Unix tail. Maybe you should That isn't actually the version I used .. its a bit lower on the page you cited above... but I can't see anything that make it work any different. cat fltr_sl [...] use File::Tail; my ($file,$line); my $fname_in = /var/adm/slpipe; $file=File::Tail-new($fname_in); while (defined($line=$file-read)) { print $line; } try it on a normal file that you write to occasionally with another Perl program. One question, in your test you didn't actually run it against a named-pipe did you? Would that be likely to make a difference? Taking your suggestion I see the script above will ouput from a normal file. touch t1 ./filterWithFileTail.pl (edited to open ./t1) while [[ 1 ]];do echo Now you've done it t1 sleep 1 cat ~/.bash_history t1 sleep 1 done The File::Tail filter does eventually output the data coming in. You mentioned it's slower.. it seems a good bit slower here. But it will NOT ouput data from the named pipe. I ran the same while loop writing to the named pipe and still the perl filter with File::Tail won't output a thing. Anyway I have a working script... soon to modify with your suggestions. .. thanks for the help. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
About the $nextline question ~~
Hi,all: I have the text like this: xxx sum = 1, xx xx xx d_bits xxx xxx xx sum =0 xx xx xx d_bit xx My question is : How can I read the nextline after the d_bits if sum = 1? I thought it for some days, but had no result . Forgive I am maybe an newbie , please give me a hand ~~ Thanks ~~
Re: printf and zero padding
Jim Gibson jimsgib...@gmail.com writes: At 4:02 PM -0500 10/24/09, Harry Putnam wrote: With this little script, how would I manage to get the shorter timestamps zero padded using printf? I now how to get padded numbers but not when I'm pushing off the right margin too. cat script.pl #!/usr/local/bin/perl use strict; use warnings; while (my $file = shift @ARGV){ my @stat = stat $file; printf %11d %s\n,$stat[9], $file; } I am not clear on what you are asking. You would use the format conversion '%011d' to zero-pad a number to fill 11 characters, but you say you know that already. Can you give an example of your desired output? Egad... I really intended to include a few lines of ouput.. I didn't even notice I hadn't... sorry. As it is I get this: stat.pl `ls` [...] 1232649333 man.pl 994039516 mms.perl 1227284469 modulo.pl 994039516 n2mbox.pl 1207227459 next_unless.pl [...] You see some of the files have been modified long enough ago that the epochal time is enough earlier to be one digit shorter. I wanted to see: 1232649333 man.pl 0994039516 mms.perl 1227284469 modulo.pl 0994039516 n2mbox.pl 1207227459 next_unless.pl And in the course of explaining it, as happens to me pretty often.. I see my mistake... Somehow I'd gotten it into my head that since it was 1 digit short I needed to pad 1 zero... Well... thats' true.. but it needs to come at the start of an 11 character parking place so anyway, thanks for making me see the error. I seem to forget about 90% of what I know between scripts. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: About the $nextline question ~~
Majian wrote: Hi,all: I have the text like this: xxx sum = 1, xx xx xx d_bits xxx xxx xx sum =0 xx xx xx d_bit xx My question is : How can I read the nextline after the d_bits if sum = 1? $ echo xxx sum = 1, xx xx xx d_bits x1xx x2xx xx sum =0 xx xx xx d_bit xx | perl -ne'/sum\s*=\s*(\d+)/ and $sum = $1; print scalar if /d_bits/ $sum == 1' x1xx John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/