×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Tag matching/processing

Tag matching/processing

Tag matching/processing

(OP)
I have some SQL command scripts 'templates'.  Such templates have 'tag' e.g.
Something like
INSERT INTO MyTable Values(<field1>,<field2>,<field3>);;

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

I tried the following

$aSQL=~ s/<(\w+)>/$$1/eg;

Where $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??

RE: Tag matching/processing

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.


#!/usr/local/bin/perl

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

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

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

RE: Tag matching/processing

okay, are you trying to do this?:

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

#.....then do something to $str so that:
$str eq "<1> <2> <3>";

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

$baz = 1;
$foo = "baz";
foreach (keys(%main::))
{
   if (m/$foo/)
   {
      print ${$main::{$_}};
   }
}


this will print a "1".  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.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Tag matching/processing

(OP)
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?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close