Solved

perl loop and variable

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 106
collecting information 2 219
Transforming a Soap message to a simple xml message! 10 167
Excel to CSV conversion with specific columns 5 109
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 …
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…

737 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