MissPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts!

I was wondering is there a way I can do this,

like.. for i = 0 to 10

next i

so that I am able to change $name1 to $name2 ....... and also $age1 to age2 ... ?

I did a little of searching and found that there is way to work for only array ?
if ( $name1 = "") { $age1 = ""; }
EDIT:
@student = (\$name1, \$name3, \$name5, \$name7, \$name9, \$name11); @all = (\$age1, \$age2, \$age3, \$age4, \$age5, \$age6); for ($i = 0; $i = 5; $i++){ if ( $student[$i] ... ) { $all[$i] = ""; }elsif ($student[$i] ... ) { $all[$i] = ""; }elsif ($student[$i] ... ) { $all[$i] = ""; } }
I tried this but its not working.

Replies are listed 'Best First'.
Re: Perl for loop
by 1nickt (Canon) on Jun 01, 2017 at 03:59 UTC

    Hi MissPerl,

    Your question is not very clear, but let's say that you mean you want to loop through two lists of things at the same time. You could store the things in two arrays and use the same index on each array as you go through the loop. That seems to be what you were trying to do.

    my @names = ('Fred', 'Wilma', 'Barney', 'Betty'); my @ages = ( 40, 38, 42, 41 ); for my $i ( 0 .. 3 ) { print "$names[ $i ] is $ages[ $i ]\n"; }
    ... but you'd be better off storing your data in a hash, then you don't have to worry about keeping the index the same, or if the lists get out of order:
    my %flintstones = ( Fred => 40, Wilma => 38, Barney => 42, Betty => 41, ); for my $name ( keys %flintstones ) { print "$name is $flintstones{ $name }\n"; }

    Hope this helps!


    The way forward always starts with a minimal test.
      Apologize for not being clear. I've just edited my question, do you think there is a way to work out for this?

        Here's an approach to what my guess is about the gist of the EDIT section of your OP. I don't understand why you would want to do something this way; please see other posts in this thread for what I would consider to be better approaches (but again, I don't really understand what you want to do). However, this reply is in direct response to your edited OP.

        c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my ($name1, $name3, $name5, $name7, $name9, $name11) = qw(Bill Jill Phil Will Gill Al); my @student = (\$name1, \$name3, \$name5, \$name7, \$name9, \$name11) +; ;; my ($age1, $age2, $age3, $age4, $age5, $age6) = (11, 22, 33, 44, 55, 66); my @all = (\$age1, \$age2, \$age3, \$age4, \$age5, \$age6); ;; dd \@student; dd \@all; ;; for (my $i = 0; $i < @student; ++$i) { if (${ $student[$i] } eq 'Al') { ${ $all[$i] } = ${ $all[$i] } * 11; } elsif (${ $student[$i] } eq 'Phil') { ${ $all[$i] } = ${ $all[$i] } * 111; } else { print qq{no satisfying condition for index $i}; } } ;; dd \@all; " [\"Bill", \"Jill", \"Phil", \"Will", \"Gill", \"Al"] [\11, \22, \33, \44, \55, \66] no satisfying condition for index 0 no satisfying condition for index 1 no satisfying condition for index 3 no satisfying condition for index 4 [\11, \22, \3663, \44, \55, \726]
        Note the  ${ $student[$i] } syntax needed to access an array element that is a scalar reference. This code runs under Perl version 5.8.9+. What version of Perl are you using?

        Note also that a syntactic shortcut for a statement like
            my @all = (\$age1, \$age2, \$age3, \$age4, \$age5, \$age6);
        is
            my @all = \($age1, $age2, $age3, $age4, $age5, $age6);

        Update: Note also that a more Perlish, less error-prone syntax for the for-loop would be

        for my $i (0 .. $#student) { if (${ $student[$i] } eq 'Al') { ... } ... }


        Give a man a fish:  <%-{-{-{-<

Re: Perl for loop
by NetWallah (Canon) on Jun 01, 2017 at 05:42 UTC
    It sounds like you are trying to use Symbolic-references.

    If so, you should read why it is a BAD idea.

                    Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

Re: Perl for loop
by choroba (Cardinal) on Jun 02, 2017 at 08:48 UTC
    The problem is in the condition in the for loop: You use an assignment instead of a comparison.
    #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; my @student = qw( John Jane Jack Jules Jim ); my @age = ( 20, 19, 21, 22, 18 ); my $whose_birthday = 'Jane'; for (my $i = 0; $i <= $#student; $i++) { # ~~ if ($student[$i] eq $whose_birthday) { $age[$i]++; say "Happy birthday $student[$i], you're now $age[$i]!" } }

    Update: Hash is a better data structure for such a task, though.

    #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; my %age_of = (John => 20, Jane => 19, Jack => 21, Jules => 22, Jim => 18); my $whose_birthday = 'Jane'; $age_of{$whose_birthday}++; say "Happy birthday $whose_birthday, you're now $age_of{$whose_birthda +y}!";
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Perl for loop
by LanX (Saint) on Jun 01, 2017 at 14:00 UTC