Home  >  Article  >  System Tutorial  >  How to handle UTF8 with Perl on Windows console

How to handle UTF8 with Perl on Windows console

王林
王林forward
2024-01-12 09:42:05962browse

How to handle UTF8 with Perl on Windows console

How to handle UTF8 using Perl on Windows console

The console is designed to use UTF8, and the displayed fonts are ugly. What should we do when the Perl program and the files to be read by Perl use UTF8 encoding? First look at the following program:

#!/usr/bin/perl

use encoding 'utf8', STDIN=>'gb2312', STDOUT=>'gb2312';

open(FILE,"c:\\test1.txt");

binmode(FILE,":encoding(utf8)");

@lines=;

close(FILE);

for($i=0; $i

print $lines[$i];

print "\n";

}

$str="Hello";

print $str;

This program uses Perl to read a text file test1.txt, and then displays it on the console. The key is:

1. use encoding 'utf8', STDIN=>'gb2312', STDOUT=>'gb2312';

use encoding 'utf8': Specify the encoding format of the script file as utf8;

STDIN=>'gb2312', STDOUT=>'gb2312': Input and output use the GB2312 character set.

In this way, the Chinese characters in the Perl script can be output.

2:binmode(FILE,":encoding(utf8)");

Specify that the encoding format of the opened file FILE is utf8, so that the data can be correctly read into @lines.

This enables Perl to process UTF8 characters on the Windows console.

How Perl reads the last line of text that is not an empty string

The three methods are as follows:

#!/bin/perl -w

use strict;

my $str = "abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj";

## method one

my $pos = -1;

my $num = 0;

while (($pos = index($str, "#", $pos)) >-1) {

print "found at $pos\n";

$pos ;

$num ;

}

print "$num found!\n";

## Method Two

my $count = 0;

while( $str =~ /#/g )

{

$count ;

}

print $count,"\n";

Method 3

my $cc = 0;

my $tmp = 0;

if( $tmp = () = ($str =~ /#/g ) )

{

$cc = $tmp;

}

print "$cc\n";

Use Perl language: write a program to read in a string of words and output each word in a line

#/usr/bin/perl

open (IN, 'D:\\words.txt ') || die $!;

@words=();

close IN;

open (OUT,'>','D:\\wordlist.txt')or die $!;

foreach $line(@words){

$line=~s/\n//g;

@words=split /\s /,$line;

foreach $word(@words){

$word=~s/\W //;

$freq {$word} ;

print OUT $word ."=>". $freq{$word}."\n";

}

}

close OUT;

You can try this program. You can read the original text directly without having to write a word in each line of the file! If you have any questions, please contact us! ### ###Teach a perl program question: read the content from the file and the content is some text in a row ### ####!/usr/bin/perl -w### ###use strict;### ###die "perl $0 " unless(@ARGV==1);### ###open (IN,$ARGV[0]) or die "Can't open file $ARGV[0]!\n";### ###open (OUT,">data2") or die "Can't create file data2\n";### ###while(){### ###chomp;### ###my @tmp=split /\t/,$_;### ###for(my $tmpc=1;$tmpcif($tmp[$tmpc-1] eq $tmp[$tmpc]){### ###print OUT "$tmp[$tmpc-1]\@2\t\@\@\@";### ###$tmpc ;### ###next;### ###}### ###print OUT "\t$tmp[$tmpc-1]";### ###}### ###print OUT "\n";### ###}###

The above is the detailed content of How to handle UTF8 with Perl on Windows console. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete