[vienna.pm] Probleme bei Stringmanipulation

Peter Seitz jun. p.seitz at koehler-seitz.de
Sun Jan 16 09:01:08 CST 2000


* * * vienna-pm-list * * *


Hallo Perl Gurus,

ich beziehe mich mal etwas auf die Antwort von Marcel Grunauer vom 
27. Dec 1999. Leider war meine Problembeschreibung nicht ganz genau, 
aber die Antwort hat mir doch etwas weitergeholfen.

Problem: Ich habe eine Zeile aus einem RC-File fuer ein Windows-
Programm (C++), in der Strings eigebettet sind. Im String werden
Hochkommas entweder duch wiederholen "" oder durch maskieren \"
eingebettet. Ich moechte mir nun den ersten auftretenden String
ermitteln einschliesslich der eingebettetet "" oder \" Hochhkommas.

Ich habe nun nachfolgendes kleines Testprogramm geschrieben.
Das Extrahieren des Strings klappt nun ganz gut mit der Subroutine.
(Kommentare zum Code erwuenscht!!! Ich glaube, dass ich das nicht 
optimal programmiert habe, kanns momentan aber noch nicht besser).

Ich habe nun Probleme, wenn ich den ermittelten String in der 
Originalzeile durch eine andere Zeichenkette ersetzen will. 
Offensichtlich befinde ich mich da im Skalar-Modus und die 
Ersetzung wird nicht richtig durchgefuehrt.
Bei meinem 2. Beispiel stuerzt das Programm ab mit folgender 
Fehlermeldung:
/"BB:155 Speichern|+Editieren,457,45,100,1155,1158,50,100,6503"/: ?+*{} 
follows nothing in regexp  at test3.pl line 11,<DATA> chunk 2

Ich bin hier voellig ratlos. Kann mich jemand auf die richtige Spur 
lenken? Danke im voraus.

#!/usr/bin/perl -w

use strict;
while (defined (my $zeile = <DATA>)) {
  chomp $zeile;
  my $translate;
  my @values = &extract_string ($zeile,$translate);
  print "Val: zeile:'$values[0]'\n\nVal: translate:'$values[1]'\n";
  my $doof = $values[0];
  $translate = "\"" . $values[1] . "\"";
  $doof =~ s/$translate/STRING_NAME_01/;
  print "\"$doof\"\n";
  print "\n";
  my $targetlanguage = <STDIN>;
}
sub extract_string {
# extract the first quoted string from imput line and return it on
# variable $match

  my ($inputline,$match) = @_;

# find out the first appearance of ", "" and """
  my @test = split (/"/, $inputline);
  my $possingle = length $test[0];      # first appearance of "
  @test = split (/""/, $inputline);
  my $posdouble = length $test[0];      # first appearance of ""
  @test = split (/"""/, $inputline);
  my $postriple = length $test[0];      # first appearance of """
  $postriple = 0 if ($postriple == length $inputline);
print "Single:$possingle - Double:$posdouble - Triple:$postriple\n";
  my $testline = $inputline;
  my $countdoublequotes = () = ($testline =~ /("")/g);
  my $counttriplequotes = () = ($testline =~ /(""")/g);
print "Doublequote:$countdoublequotes - Triplequotes:$counttriplequotes\n";
  if ($possingle == $posdouble & $posdouble != $postriple) {
    $match = "";                        # first string in line is empty
 print "empty sttring\n";
  } else {
 print "wir tun was\n";
     if ($possingle == $postriple) {    # first appearance of """
       ($testline = $testline) =~ s/"""/"\x7f\x7f\x7f/;
     }
     if ($counttriplequotes > 1) {
       ($testline = $testline) =~ s/"""/\x7f\x7f\x7f"/;
     }
     ($testline = $testline) =~ s/""/\x7f\x7f\x7f/g;
     ($match) = ($testline =~ /"([^"]+?)"/);
     $match =~ s/\x7f\x7f\x7f/""/g;
    }
#print (defined $match && length $match ? $match : "+++");
  ($inputline,$match);
}  # sub extract_string

__DATA__
 CONTROL "Mehrere Moeglichkeiten vorhanden. Eine Markieren und  ""oeffnen"" anklicken.", -1, "STATIC", SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 6, 6, 151, 18
 CONTROL "BB:155 Speichern|+Editieren,457,45,100,1155,1158,50,100,6503", -2, "STATIC", SS_LEFT | WS_CHILD | NOT WS_VISIBLE | WS_BORDER | WS_GROUP, 147, 45, 33, 20
 CONTROL "", -1, "STATIC", SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 6, 6, 151, 18
 CONTROL """Anhang"" information", 102, "BUTTON", BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 47, 33, 112, 10
 CONTROL "Kommentiere (""> "") den Text", 107, "BUTTON", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 11, 105, 112, 10
 CONTROL "Kommentiere (""> "") den ""Text""", 107, "BUTTON", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 11, 105, 112, 10
 CONTROL "Kommentiere ("" """" "") den ""Text""", 107, "BUTTON", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 11, 105, 112, 10
 CONTROL """Kommentiere"" (""> "") den ""Text""", 107, "BUTTON", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 11, 105, 112, 10



With best compliments

           Peter Seitz
--

  Graz University of Technology, Austria - Fac. f. Civil Engineering
  mailto:seitz at bzs.tu-graz.ac.at - http://wwwbzs.tu-graz.ac.at/~seitz/

###
You are subscribed to vienna-pm-list as "Peter Seitz jun." <p.seitz at koehler-seitz.de>
http://www.fff.at/fff/vienna.pm/



More information about the Vienna-pm mailing list