how do i read input from a file into a 2D array,
the file is in the format..

5 6
101010
101010
101010
101010
101010

where 5 denotes 5 rows and 6 denotes 6 columns.I need to read it all into a 2D array but With one major requirement

the instead of 5rows and 6 columns i should have a matrix of 7rows and 8 columns with the array holding values as this

11111111
11010101
11010101
11010101
11010101
11010101
11111111

i.e i hav to fill the first row ,last row,first column and last column all with 1's

Something on the order of :

#!/usr/bin/perl
# $Id: array $
# $Date: 12.02.11 $
# $HeadURL:  $
# $Revision: 2011 $
# $Source: /array.pl $
##################################################################################
use strict;
use warnings;
use CGI::Carp;

our $VERSION = 1.00;

my $filename = 'arrayin.txt';

open my $DAT, '<', $filename or croak 'cannot open file';
my @dataa = <$DAT>;
close $DAT or croak 'cannot close SFILE';

open my $DATABASE, '>', 'arrayout.txt' or croak 'array not made.';
poparray();
close $DATABASE or croak 'array not closed.';

#rewrite array;
sub poparray {
    my $top = qq{1111111\n};
    print {$DATABASE} $top or croak 'cannot write to array';
    foreach my $i ( @dataa ) {
        chomp $i;
        my $line = '1' . $i . "1\n";
        print {$DATABASE} $line or croak 'cannot write to array';
    }
    print {$DATABASE} $top or croak 'cannot write to array';

    return;
}
exit;
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.