CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [vishwakar] Have to exclude the first and last line of the file : help me: Edit Log



Zhris
Enthusiast

Dec 24, 2010, 2:52 PM


Views: 3962
Re: [vishwakar] Have to exclude the first and last line of the file : help me

Ok I am clearer as i've taken more time to look at your code and task. However, you haven't really explained anything further from your original post, and I don't understand what is it your code isn 't doing. I don't think theres anything wrong with the code provided to you proceeding your original question. I reckon it could be a path issue specifically when moving files or there are errors in your syntax.

Can you explain what this section is meant to do:

Code
foreach $file (@files) {  
if (-f "$dir$file")
{
#$moveloc="/export/elk2/sm425k/new/"; # Make sure the directory is already made.
#$new = "$moveloc";
$old = "$dir$file";
$new = "$WORKDIR";
move($old, $new) or die "Copy Faild: $!";
}
}


From first glance, it looks as if you are moving any "files" you want to deal with (all of them), to another directory, for no particular reason. For starters, is $WORKDIR a constant, if so then you'll end up with 1 file because it will already exist after the first loop. I think you meant to do "$WORKDIR/$file" but i'm not 100% sure what you are attempting to achieve in this block of code. Another thing, ensure that your code/namespacing is consistant. I can see that you do "$dir$file" sometimes and "$dir/$file" other times. I would stick to using the latter throughout.

Well, I can't test any code today because my web server is down and i'm not on a machine with perl installed, but here is my version of your code from how I understand what you are trying to do. Don't know if it will work properly (untested), or if its helpful to you at all, but I hope its along the right lines. If you are certain the code is error-free, explain exactly what it is the code is not doing that you want it to do, or is doing that you don't want it to do:


Code
#! /usr/bin/perl 
use strict;
use warnings;
use File::Copy;

#Paths
my $datdir = 'Dir/To/Data/';
my $workdir = 'Dir/To/New/Data/Dir/';

#Retrieve and move files
my @files = grep { (-f "$datdir/$_") } glob ("$datdir/*");
foreach my $file (@files) {
my $oldloc = "$datdir/$file";
my $newloc = "$workdir/$file";
move ($oldloc, $newloc) or die "Failed to move $oldloc to $newloc: $!";
}

#Get latest file (reversed lexographical sort).
my ($latestfile, @otherfiles) = reverse sort (@files);

#Get lines
open my $inputfh, '<', "$workdir/$latestfile" or die "Failed to open $workdir/$latestfile: $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
shift @lines;
pop @lines;

#Print lines
foreach my $line (@lines) {
my @cells = split /\|/, $line;
print "@cells";
}


Merry christmas,

Chris


(This post was edited by Zhris on Dec 24, 2010, 4:05 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 2:52 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 2:55 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 2:56 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:01 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:02 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:05 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:11 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:12 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:19 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:55 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 3:57 PM
Post edited by Zhris (Enthusiast) on Dec 24, 2010, 4:05 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives