print to top of file?

Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

Hey- I was wondering how, once a file is open, to print something on the first line (moving everything originally in the document down), instead of on the last line? Thanks for any help!

------------------
Flame Hosting : http://www.flamehosting.com
NT and UNIX Hosting, Dedicated Servers, and Co-Location Programs

They have: 568 posts

Joined: Nov 1999

$array[0] will print the top line of the file if your reading it from an array.

If you wnt to move the rest of the file down you need to use the push() command.

Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

No, it's not going to be in an array. It will probably use the push() command. I looked up the push() command in Programming Perl, and it gave a really vague example:

code:

for(;  {
push @ARRAY, shift @ARRAY;
...
}[/code]

Does anyone have a snippet of code for what I want to do?

------------------
Flame Hosting : http://www.flamehosting.com
NT and UNIX Hosting, Dedicated Servers, and Co-Location Programs

[This message has been edited by Justin Stayton (edited 21 March 2000).] 

They have: 850 posts

Joined: Jul 1999

Somthing like this will work

code:

$file = "file.txt";

open(IN,"$file.txt") or die "Cannot open $file: $!";
@data=<IN>;
close(IN);

#Now each line is an element in the array @data

$newinput = "rob\n";
unshift @data, $newinput;

#now $newinput is @data[0]

open(IN,">$file")or die "Cannot open $file: $!";
print IN @data;
close(IN);
[/code]

This will open the file, put the current data in it into an array.  Than use the unshift function to add the new input (in this case, $newinput) into the @data array as the first element.  When printed back into the file, the $newinput will be first.

Hope that helps at all.

------------------
A can of SPAM is opened every 4 seconds. 
Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

Darn, the people who help in this forum are excellent. Thanks alot for both or your help. I'll try it out and see if it works.

------------------
Flame Hosting : http://www.flamehosting.com
NT and UNIX Hosting, Dedicated Servers, and Co-Location Programs

They have: 568 posts

Joined: Nov 1999

Sorry I didn't get back to you sooner with that, I was at school when I posted.

***Senior Member?***

They have: 161 posts

Joined: Dec 1999

Writing to the beginning of a file is extremely inefficient. You end up the following, and you probably forget to flock() the file.

code:

use Fcntl ':flock';  # for LOCK_EX

open FILE, "+<$file" or die "can't open $file for read/write: $!";
flock FILE, LOCK_EX;

if ($slurp_into_nasty_array) {
  @file = <FILE>;
  unshift @file, @new;
  seek FILE, 0, 0;
  print FILE @file;
}
elsif ($slurp_into_scalar) {
  # can also be nasty, for big files
  local $/;
  $file = <FILE>;
  seek FILE, 0, 0;
  print FILE @new, $file;
}
elsif ($while_loop) {
  push @file, $_ while <FILE>;
  seek FILE, 0, 0;
  print FILE @new, @file;
}
truncate FILE, tell FILE;  # for safety...
close FILE;
[/code]

But I seriously don't suggest you do this.  You should answer the question "why do I need to write to the top of a file?"  You can probably get around your problem by appending to the file, and reading the file's contents, but in reverse:

code:
use Fcntl ':flock';
open FILE, ">>$file" or die "can't append to $file: $!";
print FILE @new;
close FILE;

open FILE, $file or die "can't read $file: $!";
unshift @lines, $_ while <FILE>;
close FILE;

# @lines is now the lines of $file in the REVERSE order (most recent addition first)
[/code]

Look into this method.  You'll be happier.

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve 

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.