User since
2006-06-06
3
Artikel
BenutzerIn
hallo, everyone
i'm new in perl, i want to do the following job in perl, to convert a list to a
text. how can i do it? which format can i use to store the list? Excel? or can i
only use the pure text format?
souce:
G11 G12 G13 |G21 G22 |G31 G32 G33 |
1 0 1 | 1 0 | 1 0 1 |
0 0 1 | 0 1 | 0 1 1 |
des:
G11, G13, G21 --> G31, G33;
G13, G22 --> G32, G33;
thank you very much for your answer!
Cheng
User since
2003-08-04
14371
Artikel
ModeratorIn
You can create an Excel file with Perl (
Spreadsheet::WriteExcel or
Spreadsheet::SimpleExcel), but I can't see an advantage of Perl in your aims.
If you want to "convert" your table into a text as you have shown in your post, I would use a plain text file.
User since
2006-06-06
3
Artikel
BenutzerIn
vielen Dank für deine Antwort.
die List kann ich auch als rein Text speichern, wenn es mehr geeignet ist. kannst du mir mal zeigen, wie kann ich dieses "Übersetzung" implementieren?
danke noch mal
User since
2003-08-04
14371
Artikel
ModeratorIn
I know, the following code seems to be confusing, but if you get a better understanding of
map (perldoc -f map) and
grep (perldoc -f grep), the code is not very complicated...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my $source_file = './source.txt';
tie my @array,'Tie::File',$source_file or die $!;
my $string = $array[0];
chomp $string;
my $source = substr($string,0,index($string,'|',index($string,'|')+1));
my $dest = substr($string,index($string,'|',index($string,'|')+2));
my @sources = grep{$_}split(/[\s|]+/,$source);
my @dests = grep{$_}split(/[\s|]+/,$dest);
for(1..scalar(@array)-1){
$string = $array[$_];
my $source2 = substr($string,0,index($string,'|',index($string,'|')+1));
my $dest2 = substr($string,index($string,'|',index($string,'|')+2));
my @tmp = grep{defined $_ and $_ ne ''}split(/[\s|]+/,$source2);
my @info = map{$sources[$_]}grep{$tmp[$_]}(0..scalar(@tmp)-1);
@tmp = grep{defined $_ and $_ ne ''}split(/[\s|]+/,$dest2);
my @targets = map{$dests[$_]}grep{$tmp[$_]}(0..scalar(@tmp)-1);
print join(',',@info),' --> ',join(',',@targets),";\n";
}
untie @array;
the source file:
G11 G12 G13 |G21 G22 |G31 G32 G33 |
1 0 1 | 1 0 | 1 0 1 |
0 0 1 | 0 1 | 0 1 1 |
Edit: dann hätte ich ja auch auf Deutsch antworten können ;)\n\n
<!--EDIT|renee|1149587626-->
User since
2006-06-06
3
Artikel
BenutzerIn
du bist sooooo nett. ich muss jetzt die Code mal learnen. :blush: