ich die Textdatei habe:Parsing YAML-like Textdatei in Hash-Struktur
country = {
tag = ENG
ai = {
flags = { }
combat = { ROY WLS PUR SCO EIR FRA DEL USA QUE BGL MAH MOG VIJ MYS DLH GUJ ORI JAI ASS MLC MYA ARK PEG TAU HYD }
continent = { "Oceania" }
area = { "America" "Maine" "Georgia" "Newfoundland" "Cuba" "Bengal" "Carnatic" "Ceylon" "Tanganyika" "The Mascarenes" "The Cape" "Gold" "St Helena" "Guiana" "Falklands" "Bermuda" "Oregon" }
region = { "North America" "Carribean" "India" }
war = 50
ferocity = no
}
date = { year = 0 month = january day = 0 }
}
Was ich versuche zu tun, diesen Text in Perl-Hash-Struktur zu analysieren, so dass die Ausgabe nach Daten Dump sieht wie folgt aus:
$VAR1 = {
'country' => {
'ai' => {
'area' => [
'America',
'Maine',
'Georgia',
'Newfoundland',
'Cuba',
'Bengal',
'Carnatic',
'Ceylon',
'Tanganyika',
'The Mascarenes',
'The Cape',
'Gold',
'St Helena',
'Guiana',
'Falklands',
'Bermuda',
'Oregon'
],
'combat' => [
'ROY',
'WLS',
'PUR',
'SCO',
'EIR',
'FRA',
'DEL',
'USA',
'QUE',
'BGL',
'MAH',
'MOG',
'VIJ',
'MYS',
'DLH',
'GUJ',
'ORI',
'JAI',
'ASS',
'MLC',
'MYA',
'ARK',
'PEG',
'TAU',
'HYD'
],
'continent' => [
'Oceania'
],
'ferocity' => 'no',
'flags' => [],
'region' => [
'North America',
'Carribean',
'India'
],
'war' => 50
},
'date' => {
'day' => 0,
'month' => 'january',
'year' => 0
},
'tag' => 'ENG'
}
};
Fest programmierte Version könnte wie folgt aussehen:
#!/usr/bin/perl
use Data::Dumper;
use warnings;
use strict;
my $ret;
$ret->{'country'}->{tag} = 'ENG';
$ret->{'country'}->{ai}->{flags} = [];
my @qw = qw(ROY WLS PUR SCO EIR FRA DEL USA QUE BGL MAH MOG VIJ MYS DLH GUJ ORI JAI ASS MLC MYA ARK PEG TAU HYD);
$ret->{'country'}->{ai}->{combat} = \@qw;
$ret->{'country'}->{ai}->{continent} = ["Oceania"];
$ret->{'country'}->{ai}->{area} = ["America", "Maine", "Georgia", "Newfoundland", "Cuba", "Bengal", "Carnatic", "Ceylon", "Tanganyika", "The Mascarenes", "The Cape", "Gold", "St Helena", "Guiana", "Falklands", "Bermuda", "Oregon"];
$ret->{'country'}->{ai}->{region} = ["North America", "Carribean", "India"];
$ret->{'country'}->{ai}->{war} = 50;
$ret->{'country'}->{ai}->{ferocity} = 'no';
$ret->{'country'}->{date}->{year} = 0;
$ret->{'country'}->{date}->{month} = 'january';
$ret->{'country'}->{date}->{day} = 0;
sub hash_sort {
my ($hash) = @_;
return [ (sort keys %$hash) ];
}
$Data::Dumper::Sortkeys = \hash_sort;
print Dumper($ret);
ich ich habe ein großes Problem de zugeben mit verschachtelten geschweiften Klammern umgehen. Ich habe versucht, es zu lösen, indem ich gierige und ungegenständliche Anpassung verwende, aber es scheint, dass es nicht den Trick gemacht hat. Ich habe auch über erweiterte Muster gelesen (wie (? PARNO)), aber ich habe absolut keine Ahnung, wie man sie in meinem speziellen Problem verwendet. Die Reihenfolge der Daten ist irrelevant, da ich die Unterroutine hash_sort habe. Ich werde jede Hilfe applaudieren.
Was erstellt die Textdatei. Meine Lösung wäre, eine Möglichkeit zu finden, die Textdatei so zu erstellen, dass sie wirklich eine YAML-Datei ist. Es ist verrückt, es anders zu machen! Es ist einfacher, es in einem Standardformat zu erstellen, und es ist einfacher, es zu lesen! –
Paradox Savefiles, nicht wahr? – Oesor
Europa Universalis um genau zu sein;). Aber es geht nicht um das Hacken von Spielen, sondern darum, ein Collage-Projekt zu machen. – marooou