?
Solved

perl loop and variable

Posted on 2013-12-05
5
Medium Priority
?
275 Views
Last Modified: 2013-12-05
Hi,
Please see code below.
I'm trying to check the value of the $emp1, $emp2, etc... variables by using a for loop.
I thought the best way to do it (there are actually 15 different emp variables not just 4) would be to use a loop.
The error I'm getting is:
----------
Scalar found where operator expected at t.pl line 10, near "$emp$i"
(Missing operator before $i?)
-----------

I'm not understanding that error or why this isn't working. Any suggestions how I can modify the code below to make it work the way I would like?


$emp1=0;
$emp2=1;
$emp3=22;
$emp4=0;


for($i=1;$i<5;$i++){

#I thought $emp$i would resolve to  $emp1, $emp2, $emp3, etc....  but it's not.
        if($emp$i > 0){  # Error on this line
        print "emp holds a value greater then zero\n";
        }#end of if

  }#end of for
0
Comment
Question by:nachtmsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 39698216
you can use ${"emp$i"}, though I do not recommend that technique
0
 
LVL 1

Author Comment

by:nachtmsk
ID: 39698236
Thanks ozo. Why don't your recommend it?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39698244
See `perldoc perlref`
       This is powerful, and slightly dangerous, in that it's possible to
       intend (with the utmost sincerity) to use a hard reference, and
       accidentally use a symbolic reference instead.  To protect against
       that, you can say

           use strict 'refs';

       and then only hard references will be allowed for the rest of the
       enclosing block.  An inner block may countermand that with

           no strict 'refs';
0
 
LVL 84

Expert Comment

by:ozo
ID: 39698310
Why not use

@emp=(undef,0,1,22,0);

for( @emp[1..4] ){

#I thought $emp$i would resolve to  $emp1, $emp2, $emp3, etc....  but it's not.
        if( $_ > 0 ){  # Error on this line
        print "emp holds a value greater then zero\n";
        }#end of if
0
 
LVL 1

Author Comment

by:nachtmsk
ID: 39698331
That's a good idea. Thanks.
I went ahead and put in 15 if statements. All I needed to do was push some values into an array if the emp$x id had a value > 0.
 Not elegant, but it works fine. If I have time I'll go back and change it to above. In a bit of a time crunch.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question