Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

extract words from specific location

by torres09 (Acolyte)
on Jun 07, 2013 at 04:48 UTC ( #1037571=perlquestion: print w/ replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:

Hey
while ($c2<=$#tst_num_temp) { $tst_num[$c2]=$words[$tst_num_temp[$c2]+$1]; $c2++; }

the $tst_num_temp is the array that contains location of elements and @words is the array with the elements. Now I wish to extract the element from the array @words form the location next to what is specified by the entries of @tst_num_temp . so what is wrong with what I have done , thanks

Comment on extract words from specific location
Download Code
Re: extract words from specific location
by smls (Friar) on Jun 07, 2013 at 05:09 UTC

    +$1 should be +1. Other than that, the code should work.

    If it still doesn't, the error is probably in the preceding part that you have not shown (where you initialize the variables).

      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

        "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.

Re: extract words from specific location
by 2teez (Priest) on Jun 07, 2013 at 05:27 UTC

    Why not use a for loop like so:

    use warnings; use strict; my @alpha = qw(a b c d e f); my @location = qw(0 2 4); print $alpha[$_+1] for @location; # prints bdf
    Update:
    NOTE: Another thought, can the array with location be larger than the array that contains words to find?
    If so, then the solution adds just a simple if statement.
    my @alpha = qw(a b c d e f); my @location = qw(0 2 4 6 8 10 12); for (@location){ print $alpha[$_+1] if defined $alpha[$_+1]; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037571]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2014-07-11 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (230 votes), past polls