I have to read a bin file in perl/tk. I have been able to read and open the file, but the read in file only has every other line from the original file. I am using following to open the file.

my @contents;
open(FILE,"example_data4.bin") or die"cant open file: $!";
@contents = <FILE>;
$txt->insert('end',@contents);

But this program only reads 1,3,5,7..... lines from the original file.

Can anyone help.

Thanks

I ran a quick test and had the same problem you did; it would only get the odd numbered lines. I do not have much experience with Tk, but I think second argument to insert must be scaler. Thus, this will get all your lines:

#! /usr/bin/perl
use Tk;

$mw = MainWindow->new;
$txt = $mw->Text ()->pack ();
open my $fh, '<', 'test.txt' or die $!;
@contents = <$fh>;
foreach my $line (@contents) {
        $txt->insert ("end", $line);
}
MainLoop;

From reading "man Tk::Text" (I assume that's the widget you're using), it would appear that every other line in your unrolled array was treated as a taglist rather than character-text. (I've only ever stuffed text as single-line scalars into a Text widget, but I went looking for what could be causing your problem.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.