How to fix the utf8 issue for CSV read process - perl

I have read and got the values from the CSV file. Sometimes I have faced the character encoding issue (uf8). Review my below sample code.
my $value = 'Storno lt. Gläubiger';
$value = encode( 'iso-8859-1', $value );
Result return as
Storno lt. Gl�ubiger
Storno lt. Gl\xE4ubiger
My expecting result is "Storno lt. Gläubiger"
How to fix the issue and get the expecting result.

Well, your question is not complete. You did not provide minimal code snippet to understand what you try to do.
You need to take in consideration your output device (probably a screen) which should support utf-8 output.
You did not provide any clue what OS (Operating System) you run the code in.
For example if you use Windows 10 (probably earlier version also will work) and try to get output into cmd terminal -- then before output you have to change code page to utf-8 which can be achieved with chcp 65001 command.
If you are in Windows then in cmd terminal window type help chcp and read some documentation.
If you write perl code and inside of the code you use utf-8 strings then you need add use utf8 in the beginning of your perl code.
Change default code page of Windows console to UTF-8

Related

Issues printing ZPL via powershell

Issue printing ZPL using powershell
I'm struggling to print a label based on ZPL commands via Powershell.
Im using this line to print the labels:
Get-Content ($folder + $labelname + '.txt')|Out-Printer -Name "Generic / Text Only"
When I run this an empty label comes out. I did some tests to narrow it down.
When I open the .txt file with notepad and print it manually, the label comes out fine
So it seems there aren't any issues with the syntax of the zpl commands.
The simple conclusion I made at that point, was that my powershell line isn't working.
To check this I used a simple zpl expample and stored it in a .txt file. When I used the same powershell line to print this label, it worked.
From this I concluded that there is something in the ZPL commands which causes problems for powershell. So I compared the ZPL commands and noticed that the non-working ZPL file uses ~DGR:TFORMer.GRF (see full content below) to generate an image.
Content of nonworkinglabel.txt:
^FXParcel_Std^FS
^MMT
^JMA
~DGR:TFORMer.GRF,121160,104,
zzzzzzzzzzzzzzzzzzzzlG03SFEg0jKF8jM0....FpM02zzzzzzzzzzzzzzzzzzznR0
^XA
^PW832
^LH0,0
^LS0
^LT0
^LL1165
^FO0,0
^XGTFORMer.GRF,1,1^FS
^XZ
^XA
^IDTFORMer.GRF^FS
^XZ
I left out some of the line underneath ~DGR:TFORMer.GRF,121160,104, because the page kept crasing
What do I have to change to my powershell script to make this work? Any help would be very welcome.

Persian font encoding in m-files on Matlab [duplicate]

I'd like to use Unicode characters in comments in a MATLAB source file. This seems to work when I write the text; however, if I close the file and reload it, "unusual" characters have been turned into question marks. I guess MATLAB is saving the file as ASCII.
Is there any way to tell MATLAB to use UTF-8 instead?
According to http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995
feature('DefaultCharacterSet', 'UTF8')
will change the encoding to UTF-8. You can put the line above in your startup.m file.
How the MATLAB Process Uses Locale Settings shows how to set the encoding for different platforms. Use
feature('DefaultCharacterSet')
You can read more about this undocumented function here. See also this Matlab Central thread for other options.
Mac OSX only!
As I found solution which worked in my case I want to share it.
Mathworks advises here to use slCharacterEncoding(encoding) in order to change the encoding as desired, but for the OSX this does not solve the issue exactly as the feature('DefaultCharacterSet') in accepted answer does not do it. What helped me to get the UTF-8 encoding set for opening and saving .m files was the following link on MATLAB answers:
https://www.mathworks.com/matlabcentral/answers/12422-macosx-encoding-problem
Matlab seems to ignore any value set in slCharacterEncoding(encoding) or feature('DefaultCharacterSet') but uses region set in System Preferences -> Language & Region. After checking which region is selected in our case then it is possible to define the actual encoding in the hidden configuration file in
$matlabroot/bin/lcdata.xml
This directory can be opened by getting to the Applications and after right click on Matlab by selecting Show Package Contents as on screenshot (here in German)
For example for German default ISO-8859-1 it is possible to adjust it by changing the respective line in the file lcdata.xml:
<locale name="de_DE" encoding="ISO-8859-1" xpg_name="de_DE.ISO8859-1">
to:
<locale name="de_DE" encoding="UTF-8" xpg_name="de_DE.UTF-8">
If the region which is selected is not present in the lcdata.xml file this will not work.
Hope this helps!
The solution provided here worked for me on Windows with R2018a.
In case link doesn't work: the idea is to use file matlabroot/bin/lcdata.xml to configure an alias for encoding name (some explanation can be found in this very file in the comments):
<codeset>
<encoding name="UTF-8">
<encoding_alias name="windows-1252" />
</encoding>
</codeset>
You would use your own value instead of windows-1252, currently used encoding can be obtained by running feature('locale').
Although, if you use Unicode characters in help comments, the help browser does not recognize them, as well as console window output.
For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.
Recap here.
Check the default language in Matlab by typing in the command window getenv('LANG'). Mine returned en_US.ISO8859-1.
In the Application directory find Matlab, show its package contents. Go to bin, open lcdata.xml as an administrator, locate the corresponding xpg_name, in my case en_US.ISO8859-1. Change encoding in the same line to UTF-8. Save it.
Restart Matlab, and it's all done!

Executing Perl script inside IntelliJ Idea

I'm trying to use IntelliJ Idea to work on a perl script. I installed the perl plugin for IDEA as well as Strawberry Perl for Windows (10).
The syntax highlighting and other features work, but this is what happens when I try to run the program:
I'm a beginner at perl, so I'm still just using command line stuff and ASCII graphics. I was hoping to simply run, test, and debug my perl programs in IntelliJ, but the print statement isn't putting anything in this dialog when perl runs...
I can still type stuff in the above image where the cursor is, and typing the wrong stuff can throw errors as it is supposed to. So <STDIN> seems to be properly mapped, but <STDOUT> is not? Or is something else wrong?
If I copy the command that it shows it is running to cmd, it works perfectly:
So is there a way to configure intellij to get <STDOUT> in this dialog? Any help would be appreciated.
Now, following Chankey's answer, I've made a test script, and that one works:
Maybe the problem is that I imported the perl file from an external source, and IntelliJ doesn't realize it is a script, or something like that? But if I copy the code into the new file, it doesn't work either...
The test.pl had one difference: use warnings FATAL => 'all'; instead of use warnings;. When I try this, it works until I type a string where numeric input is expected, and then prints a whole bunch of what it should have been printing all along as it quits the program:
Again, the output works fine in cmd, so I think this is an issue with IntelliJ. Any ideas? Maybe the # symbols are creating issues? Something else? If anyone uses IDEA for windows and wants to toy around with my code, it's available on github.
Works fine for me. Below are the steps:
Create a project
Right click on project and select New, then select Perl5 file
Give a name to file and select Script from dropdown
Write your script, save it
Press CTRL+SHIFT+F10
Provide some data as user input then press Enter
Output:
If it still does not work for you then perhaps you have not setup your Perl properly.
Go to File->Project Structure and add perl to Project SDK.
As per your screenshot I can see that you are using absolute path for perl.exe. That means you have not added perl to your PATH. That seems to be the issue why IntelliJ is not finding it to execute your script.
Do
set PATH=C:\Strawberry\perl\bin;%PATH%
If that also doesn't fix the issue for you then go through the wiki page of Camelcade plugin. Author has provided step by step instructions there on how to setup this plugin.
Try adding:
$| = 1;
...to the top of your script. Perl buffers stdout, adding $| = 1; causes the buffer to flush immediately and should display output from your print statements in the IntelliJ debug console.
See also:
perl $|=1; What is this?

Unicode characters in MATLAB source files

I'd like to use Unicode characters in comments in a MATLAB source file. This seems to work when I write the text; however, if I close the file and reload it, "unusual" characters have been turned into question marks. I guess MATLAB is saving the file as ASCII.
Is there any way to tell MATLAB to use UTF-8 instead?
According to http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995
feature('DefaultCharacterSet', 'UTF8')
will change the encoding to UTF-8. You can put the line above in your startup.m file.
How the MATLAB Process Uses Locale Settings shows how to set the encoding for different platforms. Use
feature('DefaultCharacterSet')
You can read more about this undocumented function here. See also this Matlab Central thread for other options.
Mac OSX only!
As I found solution which worked in my case I want to share it.
Mathworks advises here to use slCharacterEncoding(encoding) in order to change the encoding as desired, but for the OSX this does not solve the issue exactly as the feature('DefaultCharacterSet') in accepted answer does not do it. What helped me to get the UTF-8 encoding set for opening and saving .m files was the following link on MATLAB answers:
https://www.mathworks.com/matlabcentral/answers/12422-macosx-encoding-problem
Matlab seems to ignore any value set in slCharacterEncoding(encoding) or feature('DefaultCharacterSet') but uses region set in System Preferences -> Language & Region. After checking which region is selected in our case then it is possible to define the actual encoding in the hidden configuration file in
$matlabroot/bin/lcdata.xml
This directory can be opened by getting to the Applications and after right click on Matlab by selecting Show Package Contents as on screenshot (here in German)
For example for German default ISO-8859-1 it is possible to adjust it by changing the respective line in the file lcdata.xml:
<locale name="de_DE" encoding="ISO-8859-1" xpg_name="de_DE.ISO8859-1">
to:
<locale name="de_DE" encoding="UTF-8" xpg_name="de_DE.UTF-8">
If the region which is selected is not present in the lcdata.xml file this will not work.
Hope this helps!
The solution provided here worked for me on Windows with R2018a.
In case link doesn't work: the idea is to use file matlabroot/bin/lcdata.xml to configure an alias for encoding name (some explanation can be found in this very file in the comments):
<codeset>
<encoding name="UTF-8">
<encoding_alias name="windows-1252" />
</encoding>
</codeset>
You would use your own value instead of windows-1252, currently used encoding can be obtained by running feature('locale').
Although, if you use Unicode characters in help comments, the help browser does not recognize them, as well as console window output.
For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.
Recap here.
Check the default language in Matlab by typing in the command window getenv('LANG'). Mine returned en_US.ISO8859-1.
In the Application directory find Matlab, show its package contents. Go to bin, open lcdata.xml as an administrator, locate the corresponding xpg_name, in my case en_US.ISO8859-1. Change encoding in the same line to UTF-8. Save it.
Restart Matlab, and it's all done!

PowerShell scripts on GitHub

PowerShell $OutputEncoding defaults to ASCII. PowerShell by default represents strings in Unicode. When I create scripts using the ISE, they are created in Unicode.
The following command sends text to a file in Unicode:
echo Testing > test.txt
When I push these files to GitHub, the code view chokes on them because they aren't UTF-8.
I'm confused about the best solution, with the least amount of work during commit.
I know I could convert each file and then commit, but that seems cockeyed to me. Clearly, I can't change how PowerShell represents strings internally, nor would I want to.
What are others doing?
ISE preserves an existing file's encoding but when you create a new file with ISE it is always creates the file with Unicode encoding. This has nothing to do with the $OutputEncoding. IIRC it was deemed a bug late in the dev cycle - too late to fix. Anyway, you can work-around this by going to the command window the first time you save a file and execute:
$psISE.CurrentFile.Save([Text.Encoding]::ASCII)
After that, you can just press the Save button.