Solved

Move a Line to end of file

Posted on 2014-04-08
4
308 Views
Last Modified: 2014-04-08
I am very new to Perl.  Here is what I would like to do:

Read a file in.
If File is empty, simply end (this part works)
If file has entries, move the first line to the last line. (this part isn't working).

Error: Global symbol "$first_line" requires explicit package name at ./test_move.pl line 27.

Here is my code. Any assistance is greatly appreciated!!

#!/usr/bin/perl
############################################################################################################
# Test Reading File
############################################################################################################

############################################################################################################
# Variables Section
############################################################################################################
use strict;
use warnings;
my $filename = 'filein.txt';

if ( -z "$filename" ) {
   print "File has zero size\n";

} else {

open(FH, '<', $filename)  or  die "Failed to read file $filename : $! \n";
my $firstline = <FN>;
my @remaining_lines = <FN>;
chomp($firstline);
print "The first line is $firstline\n";
close(FN);

open(FH, '>', $filename)  or  die "Failed to read file $filename : $! \n";
print FN @remaining_lines;
print FN $first_line;
close(FN);
}


Contents of Filein.txt:
Orange
Blue
Green

Would like it to end up:
Blue
Green
Orange
0
Comment
Question by:PAH_in_MI
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39985933
perl -Mdiagnostics -w test_move.pl
Global symbol "$first_line" requires explicit package name at test_move.pl line 27.
Execution of test_move.pl aborted due to compilation errors (#1)
    (F) You've said "use strict" or "use strict vars", which indicates
    that all variables must either be lexically scoped (using "my" or "state"),
    declared beforehand using "our", or explicitly qualified to say
    which package the global variable is in (using "::").
   
Uncaught exception from user code:
      Global symbol "$first_line" requires explicit package name at test_move.pl line 27.
      Execution of test_move.pl aborted due to compilation errors.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39985936
Did you mean $firstline?

And did you mean FH when you said FN?
0
 

Author Comment

by:PAH_in_MI
ID: 39985977
Good gosh.......a dang typo.  :(

Thanks for catching that.......I must need new glasses!
0
 

Author Closing Comment

by:PAH_in_MI
ID: 39985982
Save me hours of frustration....all because I can't tell the different between an H and an N.

Thanks for catching it!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now