For Loop

i have several variables (e.g file1, file2, file3 and so on)
how do i use a FOR LOOP that loops 10 times and each time it goes into the LOOP, the variable changes from file1 to file2 to file3 and so on?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
If you just want to access the variable contents:

@files = ($file1,$file2,$file3);
# get each element from array in sequence
foreach $file_n (@files) {
    $x = $file_n;
    # do something with $x

If you want to change the values of $file1 etc you will need to use variable references.
# If you want to change the values of $file1 etc, you can just change $file_n in
foreach $file_n ($file1,$file2,$file3){
  # do something with $file_n
#if you just want the litteral strings file1, file2, file3 and so on, you could use
foreach $x ( "file1".."file9" ){
    # do something with $x
irene79Author Commented:
sorry i got the answer already.
but can i ask ..
if i have a if else statement inside a for loop .. how do i break inside a if statement and cont on to the for loop?

and can i noe the syntax for "die"?
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

What do you want to do with an if else statement inside a for loop ?
Do you want the last, or next statement?

perldoc -f die
irene79Author Commented:
for eg.

for($var1=1;$var1 <10; $var1++){
  if ($var1 == 4)
     get out from here and continue with counter 5-10. acts something like a BREAK to go to the next counter.

i can use perldoc -f die in anywhere i wanna quit the program?

u gave me this code b4 .. is there any difference? die $@||"invalid date"

i'll increase the points for u .. thanx for ur help ozo :)
for $var1 ( 1..9 ){
  next if $var1 == 4;

`perldoc -f die`
    die LIST
            Outside an `eval()', prints the value of LIST to
            `STDERR' and exits with the current value of `$!'
            (errno). If `$!' is `0', exits with the value of `($? >>
            8)' (backtick `command` status). If `($? >> 8)' is `0',
            exits with `255'. Inside an `eval(),' the error message
            is stuffed into `$@' and the `eval()' is terminated with
            the undefined value. This makes `die()' the way to raise
            an exception.

            Equivalent examples:

                die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news';
                chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

            If the value of EXPR does not end in a newline, the
            current script line number and input line number (if
            any) are also printed, and a newline is supplied. Hint:
            sometimes appending `", stopped"' to your message will
            cause it to make better sense when the string `"at foo
            line 123"' is appended. Suppose you are running script

                die "/etc/games is no good";
                die "/etc/games is no good, stopped";

            produce, respectively

                /etc/games is no good at canasta line 123.
                /etc/games is no good, stopped at canasta line 123.

            See also `exit()' and `warn()'.

            If LIST is empty and `$@' already contains a value
            (typically from a previous eval) that value is reused
            after appending `"\t...propagated"'. This is useful for
            propagating exceptions:

                eval { ... };
                die unless $@ =~ /Expected exception/;

            If `$@' is empty then the string `"Died"' is used.

            You can arrange for a callback to be run just before the
            `die()' does its deed, by setting the `$SIG{__DIE__}'
            hook. The associated handler will be called with the
            error text and can change the error message, if it sees
            fit, by calling `die()' again. See the section on
            "$SIG{expr}" in the perlvar manpage for details on
            setting `%SIG' entries, and the section on "eval BLOCK"
            for some examples.

            Note that the `$SIG{__DIE__}' hook is called even inside
            eval()ed blocks/strings. If one wants the hook to do
            nothing in such situations, put

                    die @_ if $^S;

            as the first line of the handler (see the section on
            "$^S" in the perlvar manpage).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
irene79Author Commented:
thanx ozo
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.