如何在Windows控制台上利用Perl处理UTF8

王林
Freigeben: 2024-01-12 09:42:05
nach vorne
886 人浏览过

如何在Windows控制台上利用Perl处理UTF8

如何在Windows控制台上利用Perl处理UTF8

制定控制台使用UTF8,显示出来的字体也很难看,当Perl程序和Perl要读取的文件都使用UTF8编码时,怎么办呢?先看如下程序:

#!/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="你好";

print $str;

该程序用Perl去读取一个文本文件test1.txt,然后显示到控制台上。关键在于:

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

use encoding 'utf8' : 指定该脚本文件的编码格式为utf8;

STDIN=>'gb2312', STDOUT=>'gb2312': 输入和输出采用GB2312字符集。

这样,Perl脚本中的的汉字即可被输出。

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

指定打开的文件FILE的编码格式为utf8,这样就能正确的读入数据到@lines中了。

这样就实现了Perl在Windows控制台上处理UTF8字符了。

Perl如何读取文本的最后一行非空字符串

三个方法如下:

#!/bin/perl -w

use strict;

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

########### 方法一

my $pos = -1;

my $num = 0;

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

print "found at $pos\n";

$pos++;

$num ++;

}

print "找到$num个!\n";

###############################################################

# 方法二

my $count = 0;

while( $str =~ /#/g )

{

$count ++;

}

print $count,"\n";

##########################################

# 方法3

my $cc = 0;

my $tmp = 0;

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

{

$cc += $tmp;

}

print "$cc\n";

用Perl语言:写一个程序读入一串单词一个单词一行输出每一

#/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;

你试一下这个程序,文件可以不用每行一个单词,直接读取原文本就好!有问题再交流!

教一个perl程序题:从文件中读取内容内容就一排是一些文字

#!/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";

}

以上是如何在Windows控制台上利用Perl处理UTF8的详细内容。更多信息请关注PHP中文网其他相关文章!

Quelle:docexcel.net
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!