Chinaunix
标题:
请各位指点下
[打印本页]
作者:
kejiake
时间:
2012-06-28 11:03
标题:
请各位指点下
我现在在处理一个文本文件,文件每一行大概是
F1(0)=sfasf12.34sdf|F2(0)=sf-23csf45|F3(0)=6|F3(1)=sa8|F4(0)=rdsfgfd
现在要做成一个hash,key是F1,F2,F3,值分别是后面的第一个数字,如果key相同,值要相加,例如上面的是:
F1=>12.34
F2=>-23
F3=>14
F4=>
不知道该怎么搞,请各位指点下。谢谢。
PS:CU这两天出啥状况了?
作者:
kejiake
时间:
2012-06-28 11:51
my %test_new;
$c = "F1(0)=sfasf12.34sdf|F2(0)=sf-23csf45|F3(0)=6|F3(1)=sa8|F5(0)=rdsfgfd";
%test = split /\||=/, $c;
while ( ( my $key, my $value ) = each %test ) {
$key =~ /F(\d+)/;
my $key_new = $&;
$value =~ /-?(\d+)\.?(\d*)/;
my $value_new = $&;
## my %test_new = ( $key_new => $value_new );
if ( exists $test_new{$key_new} ) {
##$test_new{$key_new} = $test_new{$key_new} + $value_new;
%test_new = ( $key_new => $test_new{$key_new} + $value_new );
}
else {
%test_new = ( $key_new => $value_new );
}
while ( ( my $a, my $b ) = each %test_new ) {
print "$a=>$b\n";
}
}
F3=>8
F1=>12.34
F5=>F5
F3=>6
F2=>-23
总是F3的加不上去。。。
作者:
xiyoulaoyuanjia
时间:
2012-06-28 11:59
#!/usr/bin/perl -w
use strict;
use warnings;
my %hashData;
my $number=0;
while (<DATA>) {
while(s/(.*?)\|//){
$1 =~/F(\d)\(\d*\)=[a-z]*(\-?\d*\.?\d*)/;
$number=$2;
$number=0 if $2 eq "";
if(exists $hashData{"$1"}){
$hashData{"$1"}+=$number;
# print $2;
}else{
$hashData{"$1"}=$number;
}
}
$_ =~/F(\d)\(\d*\)=[a-z]*(\d*\.?\d*)/;
$number=$2;
$number=0 if $2 eq "";
if(exists $hashData{"$1"}){
$hashData{"$1"}+=$number;
}else{
$hashData{"$1"}=$number;
}
}
while ( my ($key,$value) = each %hashData){
print "$key => $value \n";
}
__DATA__
F1(0)=sfasf12.34sdf|F2(0)=sf-23csf45|F3(0)=6|F3(1)=sa8|F4(0)=rdsfgfd
复制代码
./lianxi.pl
4 => 0
1 => 12.34
3 => 14
2 => -23
作者:
cdtits
时间:
2012-06-28 12:32
本帖最后由 cdtits 于 2012-06-28 13:28 编辑
#!/usr/bin/env perl
%fdict;
while (<DATA>) {
while (/(F\d+).*?=.*?(-?\d+(?:\.\d+)?).*?(?:$|(?=\|))/g) {
$fdict{$1} += ($2 || 0);
}
}
while (($k, $v) = each %fdict) {
print "$k => $v\n";
}
__DATA__
F1(0)=sfasf12.34sdf|F2(0)=sf-23csf45|F3(0)=6|F3(1)=sa8|F4(0)=rdsfgfd
复制代码
作者:
cdtits
时间:
2012-06-28 13:52
本帖最后由 cdtits 于 2012-06-28 13:52 编辑
#!/usr/bin/env perl
use strict;
use warnings;
my %fdict;
while (<DATA>) {
my @datas = split /\|/;
foreach my $d (@datas) {
my ($k, $v) = split /=/, $d;
$k =~ s/\(.*\)//;
$v =~ s/.*?(-?\d+(\.\d+)?).*/$1/;
$v =~ s/[^-.\d]+//g;
$fdict{$k} += $v || 0;
}
}
while (my ($k, $v) = each %fdict) {
print "$k => $v\n";
}
__DATA__
F1(0)=sfasf12.34sdf|F2(0)=sf-23csf45|F3(0)=6|F3(1)=sa8|F4(0)=rdsfgfd
复制代码
作者:
xmchenb
时间:
2012-06-28 15:51
use strict;
use warnings;
my $filename = "d:/cc/cc.txt";
my %h;
open FILE,$filename ||die '$!';
while(my $LINE=<FILE>)
{
my @LINE=split /\|/,$LINE;
#print $LINE;
if($LINE=~/(\w+\(\d+\))(=[a-z]+)(\d+.\d+)/)
{
$h{$1}=$3;
}
}
close FILE;
while((my $k,my $v) = each %h)
{
print "$k,$v\n";
}
作者:
kejiake
时间:
2012-06-28 16:23
谢谢各位。我已经解决了
my %test_new;
my $key_new;
my $value_new;
open( FILEIN, "test0622.data" ) || die("test0622.data file not find!");
while (<FILEIN>) {
%test = split /\||=/;
while ( ( my $key, my $value ) = each %test ) {
if ( $key =~ /(F\d+)/ ) {
$key_new = $1;
}
if ( $value =~ /(\-?\d+\.?\d*)/ ) {
$value_new = $1;
}
else {
$value_new = 0;
}
if ( exists $test_new{"$key_new"} ) {
$test_new{"$key_new"} += $value_new;
}
else {
$test_new{"$key_new"} = $value_new;
}
}
while ( ( my $a, my $b ) = each %test_new ) {
print "$a=>$b\n";
}
%test_new = ();
}
作者:
xmchenb
时间:
2012-06-28 17:16
#!D:\strawberry\perl\bin\perl5.12.3 -w
use strict;
use warnings;
my $filename = "d:/cc/cc.txt";
my %h;
open FILE,$filename ||die '$!';
while(my $LINE=<FILE>)
{
my @LINE=split /\|/,$LINE;
while(<@LINE>)
{
#print $_,"\n";
if($_=~/(\w+)(\(\d+\))=([a-z]+)(-?\d+.?\d+)/)
{
if(exists $h{"$1"})
{
$h{"$1"}+=$4;
}else{
$h{"$1"}=$4;
}
}elsif ($_=~/(\w+)(\(\d+\))=([0-9]+)/)
{
if(exists $h{"$1"})
{
$h{"$1"}+=$3;
}else{
$h{"$1"}=$3;
}
}elsif ($_=~/(\w+)(\(\d+\))=([a-z]+)([0-9]+)/)
{
if(exists $h{"$1"})
{
$h{"$1"}+=$4;
}else{
$h{"$1"}=$4;
}
}elsif ($_=~/(\w+)(\(\d+\))=([a-z]+)/)
{
if(exists $h{"$1"})
{
$h{"$1"}+="";
}else{
$h{"$1"}="";
}
}
}
}
close FILE;
while((my $k,my $v) = each %h)
{
print "$k=>$v\n";
}
-----------------------
结果:
D:\PERL练习程序>cc.pl
F4=>
F3=>14
F2=>-23
F1=>12.34
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2