shift and push question in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shift and push question in perl
# 1  
Old 06-27-2007
shift and push question in perl

hi,

another perl question,

I don't understand the below

while (<FILE>) {
push @last5, $_; #add to the end
shift @last5 if @last5 > 5 ; #take from the beginning
}

can someone please explain to me how does

shift @last5 if @last5 > 5 is taking last 5 lines from the file?
I am just not getting it,
1) I thought shift was taking item from begining, shouldn't it be the begining of the file?

2) not understanding how shift @last5 if @last5 > 5 works.. can somone let me know item by item please?

big thank you in advance
# 2  
Old 06-27-2007
Quote:
Originally Posted by hankooknara
2) not understanding how shift @last5 if @last5 > 5 works.. can somone let me know item by item please?
No, because it is the overall effect of the entire while() loop that causes the last 5 lines to be stored in the @last5 array variable, not due to that shift() function alone.

While each line is added to the array in each iteration, if the "shift() .... if ..." finds that more than 5 (maximum 6, actually) lines stored in the array, it will start removing a single line each time from the top so the count is maintained at 5. You can view this as a circular buffer. Of course, when the loop terminates, the items inside must be the last 5 lines read from the file (or may be less, if the file has < 5 lines).

If you still don't get it, put some print() in the loop to print the loop content in each iteration to see for yourself the change in content in action. Of course you can use debugger. Good only if you know how to use it, and the Perl debugger is, em, well .......
# 3  
Old 06-28-2007
I understand the latter part.. but I still don't understand why

push @last5, $_ ; statment is needed...

main:Smilietail1.pl:5): open(FILE, '<', 'gettysburg.txt') or die $!;
DB<1> s
main:Smilietail1.pl:6): my @last5;
DB<1>
main:Smilietail1.pl:8): while (<FILE>) {
DB<1>
main:Smilietail1.pl:9): push @last5, $_; # add to the end
# 4  
Old 06-28-2007
I think I understand now.. so basicalli this program goes through the whole file and rebuilds the file adding items (to the end so that first item is in the top)... and it will shift(take items from top) if it has more than 2... did I understand this correctly?


#!/usr/bin/perl -w

use strict;

open(FILE, '<', 'gettysburg.txt') or die $!;
my @last5;

while (<FILE>) {
push @last5, $_; # add to the end
print @last5;
shift @last5 if @last5 > 2; # take from the beginning
print @last5;
}

close FILE;

print "Last five lines:\n", @last5;


main::(tail1.pl:5): open(FILE, '<', 'gettysburg.txt') or die $!;
DB<1> s
main::(tail1.pl:6): my @last5;
DB<1>

1
main::(tail1.pl:9): push @last5, $_; # add to the end
DB<1>
main::(tail1.pl:10): print @last5;
DB<1>
1
2

DB<1>
2
3
main::(tail1.pl:15): close FILE;
DB<1>
main::(tail1.pl:17): print "Last five lines:\n", @last5;
DB<1>
Last five lines:
2
3

Last edited by vino; 06-28-2007 at 01:48 AM.. Reason: Disable smilies in text
# 5  
Old 06-28-2007
Quote:
Originally Posted by hankooknara
I think I understand now.. so basicalli this program goes through the whole file and rebuilds the file adding items (to the end so that first item is in the top)... and it will shift(take items from top) if it has more than 2... did I understand this correctly?
Yes, and that was exactly what I explained in my previous reply.
# 6  
Old 06-29-2007
if @last5 > 2;

this forces @last5 into a scalar context,
an @array in a scalar context yields the number of elements,

so
if @last5 > 2;

means if @last5 has > 5 elements

any better?

it's the thing with perl being context sensitive, unlike any other languages I know of.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Push Files

Hey Guys, Thanks for always being helpful, I have another issue that I need a little insight on how to fix. See the below script I have and the error I get. I don't understand why it does that, am I not using the continue correctly? #!/bin/bash -x # @(#) File: filepush.sh #... (5 Replies)
Discussion started by: gkelly1117
5 Replies

2. Homework & Coursework Questions

Need help with a Perl Script using Pop, Shift, & Push

Hello everyone, I am new to Perl and I am having some issues getting a script to work. I have to create a script that uses an array of 52 cards, "shuffles" the cards (using loops with the pop, shift, and push commands), and prints out the top five. This is not a randomizing of the array just a... (2 Replies)
Discussion started by: Hax0rc1ph3r
2 Replies

3. UNIX for Dummies Questions & Answers

Rsync push or pull?

We have a cluster of 3 web servers. I'll be updating a single master server and copying info to the other 2 slave servers. What's the best way of synching all of them? Run rsync on each of the slave servers to pull the updates from the master? Or run rsync on the master to push the updates to the... (1 Reply)
Discussion started by: gaspol
1 Replies

4. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

5. Shell Programming and Scripting

Shift Question (Perl)

I am attempting to write a script that reads each line of a file into a separate array and does some work on it then puts it all back together and I think I need to use the 'shift()' command to read each line into its own array, but I need help nesting it into a while loop (while not eof) So... (10 Replies)
Discussion started by: ifeatu
10 Replies

6. Shell Programming and Scripting

Perl - if conditions is meet, push the last field of $_ into an array

I am using a seed file shown below to separate cisco devices by ios/os type. I want to bunch all the devices based on ios/os version. Once I find a match, I only want to push the ip address into the appropriate array. Example of seedfile 8 host1 (C3500XL-C3H2S-M) 11.0(5)WC17 10.1.44.21 9... (1 Reply)
Discussion started by: popeye
1 Replies

7. Linux

FTP push

Hi I am trying to send a file form one linux server into an another linux server. I cannot do ftp get. Can anyone please assist me how can I push the file to the other server ? Thanks. (2 Replies)
Discussion started by: sureshcisco
2 Replies
Login or Register to Ask a Question