Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   query in looping an array in perl (http://www.go4expert.com/forums/query-looping-array-perl-t26195/)

perlbegin 2Jul2011 16:12

query in looping an array in perl
Hi there,

Greetings to the forum member,

My query:

I have an array consisting of following elements.

@arr=('finding','related','pages','on','the','worl d','wide','wide','web');

i want to lopp through this array and print three elements consecutively for each time the loop goes through the array.


my $txt="$arr[$i] $arr[$i+1] $arr[$i+2]";# this prints first three elements in a variable.
print "$txt/12";

This outputs:

finding related pages
related pages on
pages on the
on the world
the world wide
world wide web
wide web

Here, i dont want to print the two elements combination. i.e "wide web".

i tried to use last to stop the loop 3 elements before the end of array.

i.e last if $i is equal to arr[-2].

but dont know the exact method of implementing it..

Any suggestions on it..


sreek 12Jul2011 17:43

Re: query in looping an array in perl
In for loop you can state second argument like this

$i <$#arr-1

ansh batra 3Aug2011 12:49

Re: query in looping an array in perl
print "$a[$i] $a[$i+1] $a[$i+2]\n";
just change the for condition i.e i<(length-1)

All times are GMT +5.5. The time now is 10:49.