Leser: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
my $line='id, lang1_bez, lang1_url, lang2_bez, lang2_url, lang3_bez, lang3_url'; my @data=split_line($line); print join(', ',@$_)."\n" for(@data); sub split_line { my $line=shift; my @ret=(); my @elm=split(/,\s*/,$line); my $id=shift(@elm); while(my ($bez,$url)=splice(@elm,0,2)) { my ($lang)=$bez=~/^(.+?)_/; push(@ret,[$id,$lang,$bez,$url]); } return @ret; }
Quoteid, lang1, lang1_bez, lang1_url
id, lang2, lang2_bez, lang2_url
id, lang3, lang3_bez, lang3_url
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
37
38
39
40
41
42
43
44
45
46
47
48
49
CREATE PROCEDURE "split_string"(
IN string_in VARCHAR(255)
)
BEGIN
DECLARE id INT DEFAULT 0;
DECLARE lang VARCHAR(255);
DECLARE url VARCHAR(255);
DECLARE bez VARCHAR(255);
DECLARE start INT DEFAULT 0;
DECLARE end INT DEFAULT 0;
SET end = LOCATE(",",string_in,start);
SET id = TRIM(LEFT(string_in,end-1));
SET start=end;
the_loop: LOOP
SET end = LOCATE(",",string_in,start);
IF end > 0 THEN
SET url = TRIM(SUBSTRING(string_in,$start,end-start-1));
SET start = end;
SET end = LOCATE(",",string_in,start);
IF end > 0 THEN
SET bez = TRIM(SUBSTRING(string_in,$start,end-start-1));
SET start = end;
END IF
IF LENGTH(url) > 0 AND LENGTH(bez) > 0 THEN
SET lang = LEFT(string_in,LOCATE("_",url,0)-1);
/*
INSERT ... (id,lang,url,bez)...
*/
END IF
SET bez = "";
SET url = "";
SET lang = "";
END IF
if end = 0 OR start = 0 THEN
LEAVE the_loop;
END IF
END LOOP the_loop;
END
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DROP TABLE IF EXISTS `test`.`test`;
CREATE TABLE `test`.`test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nl_bez` varchar(255) NOT NULL,
`nl_url` varchar(255) NOT NULL DEFAULT '0',
`en_bez` varchar(255) NOT NULL,
`en_url` varchar(255) NOT NULL,
`fr_bez` varchar(255) NOT NULL,
`fr_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 COMMENT='Testdatensätze';
INSERT INTO `test` (`id`,`nl_bez`,`nl_url`,`en_bez`,`en_url`,`fr_bez`,`fr_url`) VALUES
(1,'spruiten','/spruiten','syringes','/en/syringes','seringues','/fr/seringues'),
(2,'injektie','/nl/inject','injection','/en/inject','Injection','/injection');
Quote1,'spruiten','/spruiten','syringes','/en/syringes','seringues','/fr/seringues'
Quote1,'nl','spruiten','/spruiten'
1,'en','syringes','/en/syringes'
1,'fr','seringues','/fr/seringues'
1
2
3
4
5
6
7
8
SELECT t1.id, 'nl' as lang, t1.nl_bez as bez, t1.nl_url as url
FROM test t1
UNION
SELECT t2.id, 'fr' as lang, t2.fr_bez as bez, t2.fr_url as url
FROM test t2
UNION
SELECT t3.id, 'en' as lang, t3.en_bez as bez, t3.en_url as url
FROM test t3