Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (mimi_x3):

anyone able to help with perl?

OpenStudy (mimi_x3):

I need to write a program, that is able to print out the last 2 lines of a file from the command line

OpenStudy (opcode):

I did not test the code, it should however in theory work, as there is no reason for it not to. If I might voice my opinions, why would you use Perl to do this it is impractical. ``` #!/usr/bin/perl use strict; use warnings; my $file = 'kmikazuki.txt'; open my $info, $file or die "Could not open $file: $!"; my @lines = <$info>; print @lines[@lines - 2 .. $#lines]; close $info; ``` Explanation of what I actually did: I used Perl to read all the lines from the file, in this case kmikazuki.txt into an array, and then used Perl to print the last two lines of that file. Cons: Not the most memory efficient method. Fix: There is a module for it: File::ReadBackwards, thank God for CPAN.

OpenStudy (mimi_x3):

can you explain to me print @lines[@lines - 2 .. $#lines];

OpenStudy (mimi_x3):

what print @lines[@lines - 2 .. $#lines]; mean?

OpenStudy (mimi_x3):

like tell me what it means in plain english

OpenStudy (opcode):

Sorry, I was away from my keyboard. It tells Perl to read the array and print the last two lines of the array. If you do not want to use arrays there is another method you can do, but it is significantly less memory efficient.

OpenStudy (mimi_x3):

print @lines[@lines - 2 .. $#lines]; @lines = array of lines

OpenStudy (mimi_x3):

what does [@lines -2] mean?

OpenStudy (mimi_x3):

like what does " - " mean in perl?

OpenStudy (opcode):

@lines - 2 selects which lines we want to print. - is an operator, making it print the last two lines rather than first two. To print the first two use '+'. You can switch operands in Perl safely I think.

OpenStudy (mimi_x3):

why the @line[...] at the begining?

OpenStudy (opcode):

It makes Perl print readable human text rather than something like: ARRAY(0xead938)

OpenStudy (mimi_x3):

can you explain this code to me?

OpenStudy (mimi_x3):

i need to delete it fast

OpenStudy (mimi_x3):

you saw the link yet?

OpenStudy (opcode):

I don't understand that Perl code, probably because I didn't write it and Perl code is hard for me to understand if someone else wrote it. I am an idiot basically. It seems that is Perl code doing something with command line arguments. (Hinted by @ARGV.) That's as much as I can tell.

ganeshie8 (ganeshie8):

are you on unix ?

ganeshie8 (ganeshie8):

if so, you may use the shell command `tail` for this, perl is not needed

OpenStudy (opcode):

@ganeshie8 is so right.

OpenStudy (mimi_x3):

yeah, but i had to implement the tail command. i finish it already can you guys explain to me what this means: push @files, $arg;

OpenStudy (mimi_x3):

@files = (); push @files, $arg;

OpenStudy (mimi_x3):

does @files = (); => means the file is empty?

ganeshie8 (ganeshie8):

@files is an array $arg is a scalar

ganeshie8 (ganeshie8):

yes, its like `declaring` the empty array `@files`

ganeshie8 (ganeshie8):

`@` refers to array `$` refers to scalar

OpenStudy (mimi_x3):

what does push @files, $arg; ? push the array of empty files onto the scalar argument?

ganeshie8 (ganeshie8):

its the other way, visualize below : `@files` is an empty basket `$arg` contains the name of file u want to input to ur perl program `push @files, $arg` pushes the name of file into the basket @files

ganeshie8 (ganeshie8):

|dw:1409411316143:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!