Re: How to set as record separator in a Perl one liner?
On 07/16/2012 04:05 PM, De-Jian Zhao wrote: Hi, I want to change the record separator in a Perl one liner with as the separator. However, I tried without success. The perlrun document (http://perldoc.perl.org/perlrun.html#Command-Switches) says that* ***-0*[/octal/hexadecimal/] * specifies the input record separator (|$/| ) as an octal or hexadecimal number. * *When I tried to get the octal/hexadecimal code of with oct() and hex(), I got 0. I used this number and it did not work the way I wanted (perl -00 -ne 'print if //' test.seq ). From 'perldoc -f oct' ... oct Interprets EXPR as an octal string and returns the corresponding value. ... To go the other way (produce a number in octal), use sprintf() or printf(): $perms = (stat(filename))[2] 0; $oct_perms = sprintf %lo, $perms; So it is used for converting a string into an octal value. But we can go the other way with printf and ord: perl -e 'printf %lo\n, ord(q{})' 76 Now perl will leave the input record separator on the string, but we can take that off with chop: echo '' | perl -0076 -nE 'chop,say if //' TIMTOWTDI of course, and you could also do it like this: echo '' | perl -nE 'for (split //) { say if // }' Cheers, Michael Do the functions (oct and hex) return the octal and hexadecimal value of the inputted character? The document on the web seems to give different answers. http://perldoc.perl.org/index-functions-by-cat.html#Functions-for-SCALARs-or-strings hex http://perldoc.perl.org/functions/hex.html - convert a string to a hexadecimal number oct http://perldoc.perl.org/functions/oct.html - convert a string to an octal number http://perldoc.perl.org/functions/hex.html *hex EXPR **hex* Interprets EXPR as a hex string and returns the corresponding value. http://perldoc.perl.org/functions/oct.html *oct EXPR **oct* Interprets EXPR as an octal string and returns the corresponding value. I tested the functions. The first description is possibly wrong. If I am right, which functions could be used to get the octal and hexadecimal number of a string? How should the Perl one liner be written to change the record sepatator to ? Thanks for your attention. Dejian -- Michael BraderSenior Software Engineer and Perl Person Technology/Softdev/ME Small Change Team Internode http://internode.on.net/ mbra...@internode.com.au iiNet http://iinet.net.au/ m.bra...@staff.iinet.net.au -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How to set as record separator in a Perl one liner?
Hi Dejian, On Mon, 16 Jul 2012 14:35:33 +0800 De-Jian Zhao dejian.z...@gmail.com wrote: Hi, I want to change the record separator in a Perl one liner with as the separator. However, I tried without success. The perlrun document (http://perldoc.perl.org/perlrun.html#Command-Switches) says that* ***-0*[/octal/hexadecimal/] * specifies the input record separator (|$/| ) as an octal or hexadecimal number. * *When I tried to get the octal/hexadecimal code of with oct() and hex(), I got 0. I used this number and it did not work the way I wanted (perl -00 -ne 'print if //' test.seq ). You need to do sprintf(%o, ord()) instead: $ perl -E 'say sprintf(%o, ord())' 76 oct and hex convert FROM octal or hexadecimal. sprintf(%o) converts TO octal. And is a string, and you need http://perldoc.perl.org/functions/ord.html to return its first character's numeric value. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ The Human Hacking Field Guide - http://shlom.in/hhfg CPAN thrives *because* of the unfettered uploading of shit, not in spite of it. — Andy Lester Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How to set as record separator in a Perl one liner?
Michael Brader wrote: On 07/16/2012 04:05 PM, De-Jian Zhao wrote: I want to change the record separator in a Perl one liner with as the separator. However, I tried without success. The perlrun document (http://perldoc.perl.org/perlrun.html#Command-Switches) says that* ***-0*[/octal/hexadecimal/] * specifies the input record separator (|$/| ) as an octal or hexadecimal number. * *When I tried to get the octal/hexadecimal code of with oct() and hex(), I got 0. I used this number and it did not work the way I wanted (perl -00 -ne 'print if //' test.seq ). From 'perldoc -f oct' ... oct Interprets EXPR as an octal string and returns the corresponding value. ... To go the other way (produce a number in octal), use sprintf() or printf(): $perms = (stat(filename))[2] 0; $oct_perms = sprintf %lo, $perms; So it is used for converting a string into an octal value. But we can go the other way with printf and ord: perl -e 'printf %lo\n, ord(q{})' 76 Now perl will leave the input record separator on the string, but we can take that off with chop: echo '' | perl -0076 -nE 'chop,say if //' Better to use chomp (the -l switch) instead of chop: echo '' | perl -0076nlE 'say if //' TIMTOWTDI of course, and you could also do it like this: echo '' | perl -nE 'for (split //) { say if // }' echo '' | perl -F -naE '// say for @F' :-) John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/