-1

I am new to Perl coding & am facing a situation. Here is the problem statement:

I have a file which contains some data (only numbers) in matrix form like

1 2 3 4 5 6 .......
7 9 4 6 7 8 .......
...................
...................

I have another file which contains 2 rows of data (some coordinates) like

30 50
04 09
80 90
.. ..
.. ..

These are the coordinates of data situated in the first file, I want to extract that data from the first file & paste in the 3rd row in the second file, for example in the first case I have to search the element in 30th row & 50th column in the first file & paste it in the 1st row 3rd column of the second file.

Robert P
  • 15,707
  • 10
  • 68
  • 112
Samir
  • 3
  • 1
  • I want to put the contents of first file into a two dimensional array then using split I will get the data from the second file & use that data to access that particular element in the first array. I used to do C programming where I directly used 2 dim array but here I am having difficulty in getting the syntax of how to use this array – Samir Jan 24 '13 at 19:17
  • Ok, what have you tried in Perl? – Robert P Jan 24 '13 at 19:34

1 Answers1

3

This isn't hard.

First, you will have to parse the value matrix. If the input is in the filehandle $fh, then you could do:

my @data;
while(<$fh>) {
  chomp;              # remove newline
  my @row = split;    # split the current line on whitespace
  push @data, \@row;  # put a reference to this row into @data.
}

Arrays can only contain scalars, so we have to put a reference of the row into our data matrix.

The above could be written as

my @data = map { chomp; [split] } <$fh>;

instead; the [...] produces an anonymous arrayref, and map transforms a list by applying the action in the block.

The data can be accessed like $data[$y][$x]. If you want to swap the indices, this becomes akward in Perl, but isn't impossible.

To access the data at the coordinates from the second file (I'll assume the filehandle is $fh2), we just split those lines and look up the element:

while (<$fh2>) {
  chomp;
  my ($x, $y) = split;
  say "at x=$x, y=$y there is $data[$y][$x]";
}

Be sure to use strict; use warnings; at the top of each script to catch errors. say needs perl 5.10 or later.

amon
  • 57,091
  • 2
  • 89
  • 149
  • thnx for ur help I will try it out, Hey I was trying this small script in windows #!/usr/bin/perl use strict; use warnings; open ("FH","<<","D:/V.xls"); @f = ; foreach $eachline(@f) { @x=split(' ',$eachline); print "$x[1]\t$x[2]"; } but I am getting an error in cmd line saying "Global symbol @f requires explicit package name c" . Can u help me with that? – Samir Jan 24 '13 at 19:25
  • 1
    @Samir under `strict`, Perl requires you to declare all your variables; this is a good thing. You can declare with `my` (is remotely similar to `auto` in C): `my @f = ; ... my @x = split ...`. Also, consider using "lexical filehandles": `open my $fh, "<", "D:/V.xls" or die "Can't open file: $!";` ← first arg is *not* a string; there is no `<<` open mode, added error handling. – amon Jan 24 '13 at 19:47