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 /AAAA/' 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] & 07777;
                $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 'AAAA>BBBB>CCCC' | perl -0076 -nE 'chop,say if /AAAA/'
AAAA

TIMTOWTDI of course, and you could also do it like this:

echo 'AAAA>BBBB>CCCC' | perl -nE 'for (split />/) { say if /AAAA/ }'
AAAA

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 Brader                    Senior Software Engineer and Perl Person
                                  Technology/Softdev/M&E 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/


Reply via email to