Solved

Move a Line to end of file

Posted on 2014-04-08
4
315 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 168
How to get all the API from website? 11 91
Vb script to unzip a files and rename the files 12 108
Perl Untar File 1 44
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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