Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tag matching/processing

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I have some SQL command scripts 'templates'. Such templates have 'tag' e.g.
Something like
Code:
INSERT INTO MyTable Values(<field1>,<field2>,<field3>);
;

What I want is for my Perl script to open the template and replace
Code:
field1
with $field1, etc...

I tried the following
Code:
$aSQL=~ s/<(\w+)>/$$1/eg;
Where
Code:
$aSQL
contains the template.

Unfortunately, it results in the following
<whatever> becoomes blank!!
Also, the following error is found in the command prompt-
Use of uninitialized value in substitution iterator at commandProcessor.pl line 127.

What have I done wrong? Surely $1 would have matched the tag??
 
I was not able to get the reference to work in the right side of the replace. I did get it to work by moving into a small sub as shown.

Code:
#!/usr/local/bin/perl

$field1 = 'first';
$field2 = 'second';
$field3 = 'third';

$cmd = &quot;INSERT INTO MyTable Values(<field1>,<field2>,<field3>)&quot;;
print &quot;$cmd \n&quot;;
$cmd=~ s/<(\w+
[red]?[/red]
Code:
)>/&trade_val($1)/eg;
print &quot;$cmd \n&quot;;

sub trade_val
{
my $field = shift;
return $$field;
}

You need the '?' after your '\w+' or it will match the first '<' and the last '>' and everything in between. The '+' is a 'greedy' wild card. I think you want to match the first '<', then the minimal amount of stuff to find the closing '>'. The '?' causes the minimal match.

<ponder....>You might be able to put an anonymous sub in the right side to evaluate, but, I don't have time to play, just now.
'hope this helps.


keep the rudder amid ship and beware the odd typo
 
okay, are you trying to do this?:[tt]

my ($foo, $bar, $baz) = (1, 2, 3);
my $str = &quot;<foo> <bar> <baz>&quot;;

#.....then do something to $str so that:
$str eq &quot;<1> <2> <3>&quot;;
[/tt]

well, i experimented and brainstormed, and here is what i found would work:[tt]

$baz = 1;
$foo = &quot;baz&quot;;
foreach (keys(%main::))
{
if (m/$foo/)
{
print ${$main::{$_}};
}
}[/tt]


this will print a &quot;1&quot;. if this what you're trying to do, i hope you can figure out how this applies to what you're doing. if you're having trouble doing so, ask for more help and i can make this more suited to your context.

&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Something 'weird' is going on...
My code strucure is basically
my $aField1;
my $aField2;


foreach $eachFile(@allFiles)
{
$fieldWhatever='Whatever';
$aSQL=~ s/(<w+?>/$$1/eg'
}

The above actually works once I deleted the my $field{1,2,3, etc. etc}!!

Strange... it was complaining that the substitution iterator was not initialised.. but it definitely was!
the my scope should be fine, isn't it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top