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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Interpolation (simple?) 3

Status
Not open for further replies.

duncdude

Programmer
Jul 28, 2003
1,979
GB
How would it be possible using a loop to replace the values of 1-9?

$fld1=trim ($fld1);
$fld2=trim ($fld2);
$fld2=trim ($fld2);
$fld3=trim ($fld3);
$fld4=trim ($fld4);
$fld5=trim ($fld5);
$fld6=trim ($fld6);
$fld7=trim ($fld7);
$fld8=trim ($fld8);
$fld9=trim ($fld9);

to be something like this...

while ($x < 10) {

$fld($x) = trim ($fld($x));

}

I just can't work it out!!!
 
are you trying to take out all the numbers the first number your question is vague.

anyway to google it the term is substitution if this code doesn't help.



foreach $line (@$fld){
$line =~s/ [1-9] //g;
}
 
Something like this?
Code:
@fld=split(/,/);
$loop=0;
foreach $fld (@fld) {
  $fld[$loop]=trim($fld[$loop]);
  $loop++;
}

HTH
==Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
no, sorry I didn't explain well

I would like the following but 'condensed' using a loop

$fld1=trim ($fld1);
$fld2=trim ($fld2);
$fld3=trim ($fld3);
$fld4=trim ($fld4);
$fld5=trim ($fld5);
$fld6=trim ($fld6);
$fld7=trim ($fld7);
$fld8=trim ($fld8);
$fld9=trim ($fld9);

The only thing changing is the 1..9 - could this be replaced by concatenation of a two variables?

while ($x < 10) {

$fld $x = trim ( $fld $x );

}

This loop would run through nine times and save the longer code above

Cheers
Duncan
 
no, sorry I didn't explain well

I would like the following but 'condensed' using a loop

$fld1=trim ($fld1);
$fld2=trim ($fld2);
$fld3=trim ($fld3);
$fld4=trim ($fld4);
$fld5=trim ($fld5);
$fld6=trim ($fld6);
$fld7=trim ($fld7);
$fld8=trim ($fld8);
$fld9=trim ($fld9);
^ ^
| |
|__________|________ like to replace with $x

The only thing changing is the 1..9 - could this be replaced by concatenation of two variables?

while ($x < 10) {

$fld $x = trim ( $fld $x );

}

This loop would run through nine times and save the longer code above

Cheers
Duncan
 
Got it
use eval
Code:
#!/usr/bin/perl
$fld1=&quot;one        &quot;;
$fld2=&quot;two        &quot;;
$fld3=&quot;three     &quot;;
while ($i<=3) {
  $prog=&quot;\$fld&quot;.$i.&quot;=trim(\$fld&quot;.$i.&quot;)&quot;;
  eval ($prog);
  $i++;
}
print &quot;[$fld1][$fld2][$fld3]\n&quot;;

sub trim {
  my @out=@_;
  for (@out) {
   s/^\s+//;
   s/\s+$//;
  }
  return wantarray ? @out : $out[0];
}

HTH
==Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Or use soft references.
Code:
{  # just to scope the 'no strict' 

  no strict 'refs';  # necessary if you are using strict.
  
  while ($x < 10) {
    ${&quot;fld$x&quot;} = trim ( ${&quot;fld$x&quot; );
  }
}

Hope this helps.

--jim
 
Oh, yea, I forgot to put in my opinion that this method you are using is a great candidate for re-thinking.

Use an array instead, as in Paul's example.

--jim
 
Superb! Just what I wanted to know. Thank you!

Duncan
 
Dear Coderifous

I'm sorry but I don't understand your comment...

&quot;Oh, yea, I forgot to put in my opinion that this method you are using is a great candidate for re-thinking.&quot;

Duncan
 
Duncan,

Rethinking, or refactoring as it's often referred to, is a common phase during the course of normal software development. Allow me to elaborate.

When initially presented with a problem, most developers are eager to invent a solution. Sometimes, a developer can sit down, code out a solution, and that code will never need to be reworked. That's pretty rare though. Most of the time, a developers first shot at solving a problem will give them greater understanding of the problem, and a proportionately better understanding of what solution would be appropriate FOR that problem.

So, in most cases, an interative design approach is used:
you write code,
figure out how it could be better (more scalable, more maintainable, more portable, etc...) (this is the 'refactoring')
and then rewrite the code

rinse, wash, repeat.

Specific to your code, I think it is a good candidate for rethinking because you are using soft references. In general, anytime you have to resort to soft references, it's time to rethink your approach, becuase there's almost always a better one out there. That's my experience anyways. You were using a base variable name, and appending a digit in order to store different values. In this case, a more elegant solution would be to use an array.

My comment was not meant to be condescending. I apologize if it came across that way. This forum is for programmers of all levels, and putting people down because they want to learn more is certainly not a practice I condone.

happy hacking,

--jim
 
Ah, I see! My Perl is pretty weak and I didn't quite understand your comment.

The code in my example wasn't actually mine - it was from another thread - I was just sure it could be condensed.

Duncan
 
That was me, and the paper didn't arrive so I did that instead of the crossword, could've been more elegant but it worked for the specific example given

Apologies for misleading anyone

Rgds
==Paul
 
Paul

I certainly didn't mean to diss your code. My coding is nowhere near up to your level. It's simply that I am learning Perl and am always trying to find things to test myself on as I imagine we all do!

regards
Duncan
 
no worries
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top