Solved

Move a Line to end of file

Posted on 2014-04-08
4
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
work on ods spreadsheet with perl in ubuntu 4 81
Removing file extension within a file. 4 103
rename outfile before writing 2 76
Perl script to delete older files 6 94
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

733 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