Solved

perl loop and variable

Posted on 2013-12-05
5
263 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
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

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

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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

832 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