Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Beautiful Arrays : NEED HELP!

Beautiful Arrays : NEED HELP!

Beautiful Arrays : NEED HELP!

Okay, this is the situation:
### Gets the text fil ###

$list = 'list22.txt';
@test = <INF>;

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

foreach $line (@test) {
    print "$line\n";
print "\nnext five lines being processed\n\n";

### dels the first 5 lines ###

@test= splice(@test,5);

foreach $line (@test) {
    print "$line\n";

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)

RE: Beautiful Arrays : NEED HELP!

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)

RE: Beautiful Arrays : NEED HELP!

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:

### dels the first 5 lines, puts them in @fivelines ###

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

foreach $line (@fivelines) {
    print "$line\n";

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

RE: Beautiful Arrays : NEED HELP!

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)

RE: Beautiful Arrays : NEED HELP!

When you execute

@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

@test = qw(a, b, c, d, e, f, g, h, i, j, k, l, m);
@five = splice(@test, 0, 5);
print "\@five = ";
foreach (@five) { print $_, " " };
print "\n\@test = ";
foreach (@test) { print $_, " " };
print "\n";

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close