svenXY has asked for the wisdom of the Perl Monks concerning the following question:
I have experienced some strange behaviour with Tie::File.
The following code ties to the file, adds some elements, then sorts them and unties. After that, a newline is added to the file. Those newlines add up and clutter the file.
prints:#!/usr/bin/perl use strict; use warnings; use Tie::File; tie my @tied_array, 'Tie::File', 'tiefile' or die "Could not tie to t +iefile: $!"; push(@tied_array, $_) for (1..3); print "\@tied_array has " . scalar @tied_array . " elements before sor +ting\n"; @tied_array = sort {uc($a) cmp uc($b)} @tied_array; print "\@tied_array has " . scalar @tied_array . " elements after sort +ing\n"; untie @tied_array;
~/dev/perl$ perl test_tie_file.pl @tied_array has 3 elements before sorting @tied_array has 4 elements after sorting ~/dev/perl$ perl test_tie_file.pl @tied_array has 7 elements before sorting @tied_array has 8 elements after sorting ~/dev/perl$ perl test_tie_file.pl @tied_array has 11 elements before sorting @tied_array has 12 elements after sorting ~/dev/perl$ cat tiefile 1 1 1 2 2 2 3 3 3 ~/dev/perl$
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tie::File - sorting array adds empty lines
by wojtyk (Friar) on Sep 11, 2008 at 22:44 UTC | |
by ikegami (Patriarch) on Sep 11, 2008 at 23:58 UTC | |
by demerphq (Chancellor) on Jan 30, 2020 at 09:01 UTC | |
|
Re: Tie::File - sorting array adds empty lines
by ikegami (Patriarch) on Sep 11, 2008 at 20:09 UTC | |
|
Re: Tie::File - sorting array adds empty lines (followup)
by ikegami (Patriarch) on Sep 11, 2008 at 20:29 UTC |