in reply to Re^3: Append Next Line To Current Line
in thread Append Next Line To Current Line

Instead of a foreach loop, try out a for loop so we can use index numbers on the @data array

You might not be aware that for and foreach are synonyms for each other and can be used interchangeably. You can also use a Perl-style for loop to access indexes into an array.

knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > foreach ( my $count = 0; $count < 5; $count ++ ) > { > say $count; > } > > my @arr = qw{ one two three }; > for my $elem ( @arr ) > { > say $elem; > } > > foreach my $idx ( 0 .. $#arr ) > { > say $arr[ $idx ]; > }' 0 1 2 3 4 one two three one two three knoppix@Microknoppix:~$

I hope this is helpful.

Update: Reworded first sentence slightly as it might have come over as a bit rude, which was not my intention.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^5: Append Next Line To Current Line
by Ransom (Beadle) on Jul 02, 2012 at 19:22 UTC

    Yikes, you're blowing my cover as a perl programmer ;-)

    Seriously though, I did not know they were interchangable. It's one of those "I've always done it this way" moments. It's neat to know that foreach is just a shortcut rather than something different. Thank you