Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Encoding and decoding a Unicode

by suno (Acolyte)
on Jul 10, 2015 at 10:42 UTC ( [id://1134112]=perlquestion: print w/replies, xml ) Need Help??

suno has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!

I have a powerbuilder debug file which is in unicode. I tried encoding it and did some processing. the code is as below:

#!/usr/bin/perl my $input=$ARGV[0]; open(INFILE,'<:encoding(utf-16LE)',$input) or die("Not able to open th +e input file $input $!"); while(<INFILE>) { my $currentline=$_; next if($currentline=~/^\s+(CLOSE|OPEN)\s+/i); print $currentline; }

Next is, I need to decode the modified encoded source file. I tried the below code for decoding but still got empty lines.

open my $fh, '>:encoding(utf-16LE)', $currentline; open my $fh, '<:encoding(utf-16LE)', $menufile;
I am finding it a bit tough to understand and get a solution for decoding.Please help me in this. Thanks in advance.

Replies are listed 'Best First'.
Re: Encoding and decoding a Unicode
by afoken (Chancellor) on Jul 10, 2015 at 12:31 UTC
    I tried the below code for decoding but still got empty lines.

    Some unexpected empty lines or only empty lines?

    Empty lines after (almost) all lines you read? chomp!

    #!/usr/bin/perl my $input=$ARGV[0];

    Add use strict; and use warnings;. Ignoring perl's debugging aids just wastes everyone's time.

    And finally, whenever you think that perl has encoding problems, show us a hex dump of the input and the output.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks much for your reply! :-)

      After encoding the unicode file, did some modifications (In the stated example it is all lines other than lines with 'OPEN' or 'CLOSE').

      After this process when i try to decode the encoded lines, i am getting only empty lines for all the read lines.

      I want help in decoding the encoded file.

Re: Encoding and decoding a Unicode
by vinoth.ree (Monsignor) on Jul 10, 2015 at 12:02 UTC

    Have a look into Encode module. It has lot of useful function.


    All is well. I learn by answering your questions...
Re: Encoding and decoding a Unicode
by Anonymous Monk on Jul 10, 2015 at 12:03 UTC
Re: Encoding and decoding a Unicode
by andal (Hermit) on Jul 13, 2015 at 07:25 UTC

    First of all, there's small bug in your code. When you pass content of the file through decoding process (via 'encoding' directive), you get "characters". You should not pass those characters to STDOUT, because by default it expects "octets" instead of "characters". Well, it kinda works if your text contains only English alphabet, but it won't work properly for other characters.

    Second, your question is not clear. In the second block of code you use '$currentline', which is also used in the first block, is it the same variable, or different one? Maybe you can provide real block of code that produces problems, then it would be possible to see where you make mistake. Otherwise, as already recommended, try to read 'perldoc Encode'.

Re: Encoding and decoding a Unicode
by u65 (Chaplain) on Jul 10, 2015 at 11:43 UTC

    I'm a bit confused. Is the original input file comprised of data and file names? Are both chunks of code in the same file?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1134112]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found