Thread Variable in String (5 answers)
Opened by ottto at 2012-06-06 14:26

FIFO
 2012-06-06 16:19
#158808 #158808
User since
2005-06-01
469 Artikel
BenutzerIn

user image
Hi, wenn ich es richtig verstehe, willst Du in dem String, den Du als Zeile aus der Datei liest, die Zeichenfolge '$var1' durch den Inhalt der Variable $var1 ersetzen (Template-artig?).
Prinzipiell geht das z.B. so:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
use warnings;
use strict;

my $var1 = "0815";
print "Meine Variable hat den Inhalt: $var1\n";

open(my $dat1,'<', "c:/Muster.txt") or die "Datei wird nicht gefunden";
while (my $line = <$dat1>) {
    $line =~ s{\$var1\b}{$var1}g;
    print "$line";
}
close $dat1;


edit: pq hat (während ich das hier schrieb) ja schon ausgeführt, warum so eine Lösung Nachteile hat und Du besser einen allgemeineren Ansatz mit einem Hash wählst ...
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"

View full thread Variable in String