Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks  

Re: Tie::File Deleting Lines Including "\n"

by sk (Curate)
on Oct 04, 2005 at 20:42 UTC ( #497396=note: print w/ replies, xml ) Need Help??


in reply to Tie::File Deleting Lines Including "\n"

from  perldoc -f delete

Deleting an array element effectively returns that position of the arr +ay to its initial, uninitialized state. Subsequently testing for the + same element with exists() will return false. Note that deleting ar +ray elements in the middle of an array will not shift the index of th +e ones after them down--use splice() for that. See "exists".

The array has not shrunk in size.

#!/usr/bin/perl -w my @array = (1, 2, 3, 4); print "length = ", scalar @array,$/; delete $array[2]; print "length = ", scalar @array,$/; __END__ length = 4 length = 4

If it is just the second element (index = 1) then you can also do this simple assignment and shift. If you want to move around quite freely use splice.

infile

first second third fourth

use Tie::File; my $file = "file.csv"; tie my @lines, Tie::File, $file or die "can't update $file: $!"; $lines[1] = $lines[0]; shift(@lines); untie @lines;
outfile
first third fourth


Comment on Re: Tie::File Deleting Lines Including "\n"
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://497396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-04-19 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls