Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beautiful Arrays : NEED HELP!

Status
Not open for further replies.

DMG2000

Programmer
Aug 13, 2000
52
0
0
US
Okay, this is the situation:
### Gets the text fil ###

$list = 'list22.txt';
open(INF,"$list");
@test = <INF>;

### for each line, writes the line####

foreach $line (@test) {
chomp($line);
print &quot;$line\n&quot;;

}
print &quot;\nnext five lines being processed\n\n&quot;;

### dels the first 5 lines ###

@test= splice(@test,5);

foreach $line (@test) {
chomp($line);
print &quot;$line\n&quot;;

}

What I need to do is call the first 5 lines of the text file and process them, then delete them and grab the next 5 lines, process them and delete them, ect...

However, I cant seem to get just the first 5 lines of the array and process them, using the code above, it calls all the lines in the array, but the splice does take out the first five lines after being.

Any ez solutions on this (not too famillar with perl yet, but am learning)?

Shawn (DMG2000)
compute_x@yahoo.com
 
Hmmmm...this is what happens when you stop thinking in logic:


code line should be:
foreach $line (@test[0,1,2,3,4])

I hate posting to myself, very simullar to talking to ones self.....

Shawn (DMG2000)
compute_x@yahoo.com
 
Your code is assigning @test the elements removed
from the original @test by splice. You are removing
everything from offset 5 to the end of the list.
So @test ends up being the original array without
the first 5 members.

If you want to use splice, try something like this:
Code:
### dels the first 5 lines, puts them in @fivelines ###

@fivelines = splice(@test,0,5);

foreach $line (@fivelines) {
    chomp($line);
    print &quot;$line\n&quot;;
    
}
The splice function removes from @test at offset 0 for
length 5 (the first five elements). @fivelines gets
the values removed from @test (the first five elements).
 
No, the splice does not take out from 5 to end, it only removes the first 5, this has been tried and true so I know this works.

Shawn (DMG2000)
compute_x@yahoo.com
 
When you execute
Code:
@test = splice(@test,5);
you are modifying @test twice.

First the splice command removes all elements
starting at offset 5. Then the assignment
assigns @test the array that was just removed
by splice.

Try this code to see how splice works. I am using two
different arrays. @test is modified by the splice
command, and @five gets the elements removed by the
splice:
Code:
@test = qw(a, b, c, d, e, f, g, h, i, j, k, l, m);
@five = splice(@test, 0, 5);
print &quot;\@five = &quot;;
foreach (@five) { print $_, &quot; &quot; };
print &quot;\n\@test = &quot;;
foreach (@test) { print $_, &quot; &quot; };
print &quot;\n&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top