Solved

perl loop and variable

Posted on 2013-12-05
5
265 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 162
compress files in RAR using perl 13 85
Parse csv file and generate graphs in HTML in bash 8 240
combine multiple lines 2 67
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

828 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