免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1641 | 回复: 3
打印 上一主题 下一主题

一个格式转换的问题,请帮忙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-27 07:12 |只看该作者 |倒序浏览
大虾们好。最近帮忙写一个格式转换的脚本。
  源格式是 : 行列不定
省份        城市        长途区号        1390H1H2H3        1380H1H2H3        1370H1H2H3        1360H1H2H3        1350H1H2H3
安徽        宿州        557        "557,567"        557        557        557        "058,557"
。。。。。。。。。。。。

  目标格式是 :
H码,区号,城市
1390557,557,宿州
。。。。。。。。。。。

我写个两天基本上完成了该功能。但是还是存在一些缺陷请大家帮忙看看。
1、源文件是XLS的,我是先导table分割的txt文件,然后再处理,生成目标文件;
2、感觉写的有些复杂了,能有更简介的方式吗?

我的代码如下:请大虾们帮忙了
#!F:\Program Files\Perl\bin\perl.exe
sub genHcode{
        my $HcodePatten =$_[0] ;
        my $HcodeParam =$_[1] ;
        my $AreaCode = $_[2] ;
        my $CityName = $_[3] ;
        $HcodePatten =~ s/\"|\n//g ;
        $HcodeParam =~ s/\"|\n//g ;
        my @HcodeSet ;
        my $Item;
        my $val1 = "" ;
        my $val2 = "" ;
        my $CodeHead = substr($HcodePatten,0,index($HcodePatten ,"H"));
        my $HCount = ( $HcodePatten =~ tr/H//);
        my $count = 0 ;
        my $i = 0;
        my @tmpHcodeSet=split(',',$HcodeParam) ;
        foreach $Item(@tmpHcodeSet){
                $count = ( $Item =~ tr/-//);
                if ( $count > 0 ){
                        for(($val1 ,$val2) = split('-',$Item);$val1 <=$val2 ;$val1++)
                        {       
                                for($i = $HCount - length($val1); $i > 0;$i-- )  {$val1= "0".$val1  ;}
                                push (@HcodeSet ,"$CodeHead$val1,$AreaCode,$CityName\n");
                        }
                }else
                {
                        $val1 =$Item;
                        for($i = $HCount - length($val1); $i>0;$i-- ) { $val1="0".$val1 ;}
                        push  @HcodeSet , "$CodeHead$val1,$AreaCode,$CityName\n";
                }
        }
        return  (@HcodeSet);
}
################################main#############################
print "Hello, World...\n";
########################获取文件中信息###########################
open (FILE , "D:\\CODE\\perl\\test1.txt");
my @HCodeInfo = <FILE> ;
my $RecordCount = @HCodeInfo;
close(FILE);
print " $HCodeInfo[0] \n";
#################################################################
my @HCodesets ;
my @HeadInfo = split('        ', $HCodeInfo[0]);
my $HeadInfoCount =@HeadInfo;
#################################################################
for(my $i = 1 ; $i < $RecordCount ;$i++){
        @RecordInfo = split('        ', $HCodeInfo[$i]);
        for(my $j = 3 ; $j <= $HeadInfoCount ;$j++ ){
                push @HCodesets , genHcode(  $HeadInfo[$j],$RecordInfo[$j] ,$RecordInfo[2],$RecordInfo[1] );
        }
}
open(OUTPUT, "> D:\\CODE\\perl\\testHCode.txt");
print OUTPUT  @HCodesets;
close(OUTPUT);

[ 本帖最后由 roywangt 于 2006-7-27 07:15 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-07-27 17:52 |只看该作者

[TIPS][DATA PROCESS]

Perl makes things simple.
1. load data from file into an array
open F, 'data.txt'; @lines=<F>; close F;
2. first line is header or fields title => first element in $lines[0]:
@fields_title = split /\s+/, shift @lines, 8;
later use this @fields_title as hash's key and next put resst records into hash's value as reference array
3. now process @lines that contains rest records.

  1. foreach my $line (@lines) {
  2.     chomp $line;
  3.     my @cols = split /\s+/, $line, 8;
  4.     my $count =0;
  5.     foreach my $col (@cols) {
  6.         push @{$rec{$field_title[$count]}},$col;
  7.         $count++;
  8.     }
  9. }
  10. # now you have mapped records and do any what you want.
  11. print Data::Dumper(\%rec);
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-07-31 07:02 |只看该作者

谢谢 ulmer

看了您的回帖  
========================================
2. first line is header or fields title => first element in $lines[0]:
@fields_title = split /\s+/, shift @lines, 8;
========================================
shift 的作用是什么呢?
谢谢先!

还有
===================================
# now you have mapped records and do any what you want.
print Data:umper(\%rec);
====================================
中的Data:umper 是在那个包里面。
还是谢谢先!

[ 本帖最后由 roywangt 于 2006-7-31 07:03 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-07-31 16:01 |只看该作者
A: 2.
perldoc -f shift
shift ARRAY
       shift   Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down.

A. 3.
CPAN module Data::Dumper - print variable of data structures
code example:
use Data::Dumper;
$var = [2,4,5];
print Dumper($var);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP