in reply to Re: extract words from specific location
in thread extract words from specific location

No +1 does no work either , but I got the answer we can do it like  $words[++$tst_num_temp[$c2] instead of $words[$tst_num_temp[$c2]+$1] . It works . thanks anyways

Replies are listed 'Best First'.
Re^3: extract words from specific location
by smls (Friar) on Jun 07, 2013 at 06:01 UTC

    "No +1 does no work either"

    Sure it does:

    use warnings; use strict; # 0 1 2 3 4 5 6 7 # ' ' ' ' ' ' ' ' my @words = qw(aaa bbb ccc ddd eee fff ggg hhh); my @tst_num_temp = qw(0 4 6); my $c2 = 0; my @tst_num; while ($c2<=$#tst_num_temp) { $tst_num[$c2]=$words[$tst_num_temp[$c2]+1]; $c2++; } print "@tst_num\n";

    ...prints:

    bbb fff hhh


    "but I got the answer we can do it like $words[++$tst_num_temp[$c2]"

    You can, and it will get you the exact same result for @tst_num as the +1 method. But in addition to that it will also modify @tst_num_temp, which you should be aware of in case the following code continues to use that variable.

    In any case, consider using a for loop like 2teez suggested, it's much nicer compared to a while loop with manually incremented index.