Solved

perl loop and variable

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now