Program for pairing together to print outputusing perl


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Program for pairing together to print outputusing perl
# 1  
Java Program for pairing together to print outputusing perl

Suppose u have One file

one row and one column

A1 A2 A3 A4
A1
A2
A3
A4

And another Second file shows pairing

A1 A4
A2 A3
A2 A4
A1 A3


Want the output to be like based on the pairing ..
As seen from the second fileThose who are paired will get one A1 A4 will get one
same with A1 A3 will get one
A1 A2 will get zero

A1 A2 A3 A4
A1 0 0 1 1
A2 0 0 1 1
A3 1 1 0 0
A4 1 1 0 0


What can be done ?
# 2  
You can put the first line in an array and than take the first "word" beginning with the second line and compare it against each element.
# 3  
Java

That I have tried using this code
Code:
#!/usr/bin/perl 

$data_file="aa1";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

#print $raw_data[0];

$data_file2="aa2";
open(THAT, $data_file2) || die("Could not open file!");
@raw2_data=<THAT>;
close(THAT);
foreach $data (@raw2_data)
{

@siv =split(";",$data);
if  (($siv[0])== ($siv[1]))
{
print "1";
}
else {
print "0";
}
}

However ,how to get this matrix type if u have
A1 A2 A3 A4
A1 0 0 1 1
A2 0 0 1 1
A3 1 1 0 0
A4 1 1 0 0

---------- Post updated at 12:12 PM ---------- Previous update was at 05:09 AM ----------

Quote:
Originally Posted by majormark
You can put the first line in an array and than take the first "word" beginning with the second line and compare it against each element.
Code:
use strict;
use warnings;

my %A;
my ($max_row, $max_col) = (4,4);

# autovivify interesting rows/cols of %A
while (<DATA>) {
  warn("Line $.: cannot understand: $_") , next unless /A(\d+);A(\d+)/;
  my ($row, $col) = ($1,$2);
  $A{$row}{$col}++;
  $A{$col}{$row}++; # uncomment if relationship is not bidirectional
}

# print col. header
print "    ";
printf("A%-3d",$_) for (1..$max_col);
print "\n";

#print matrix A
for my $row (1 .. $max_row) {
  printf("A%-3d", $row);

  for my $col (1 .. $max_col) {
    printf(" %-3d",  exists $A{$row}{$col} ? "1" : "0" );
  }
  print "\n";
}

__DATA__
A1;A4
A2;A3
A2;A4
A1;A3

It will give output like
A1 A2 A3 A4
A1 0 0 1 1
A2 0 0 1 1
A3 1 1 0 0
A4 1 1 0 0


If u have horse instead of A1
lion instead of A2
mule instead of A3
tiger instead of A4

__DATA__
horse;tiger
lion;mule
lion;tiger
horse;mule
Then in respone to that code tried to modify.

Code:
use strict;
use warnings;

my %A;
my ($max_row, $max_col) = (4,4);

# autovivify interesting rows/cols of %A
while (<DATA>) {
  warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/
+;
  my ($row, $col) = ($1,$2);
  
}


print "    ";
printf("%-3d",$_) for (1..$max_col);
print "\n";

for my $row (1 .. $max_row) {
  printf("%s", $row);

  for my $col (1 .. $max_col) {
    printf(" %s",  exists {$row}{$col} ? "1" : "0" );
  }
  print "\n";
}

The output shud be like

horse lion mule tiger
horse 0 0 1 1
lion 0 0 1 1
mule 1 1 0 0
tiger 1 1 0 0
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #463
Difficulty: Medium
chrony is an implementation of the Network Time Protocol (NTP) and is a replacement for the ntpd.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Pairing the nth elements on multiple lines iteratively

Hello, I'm trying to create a word translation section of a book. Each entry in the word list will come from a set of linguistically analyzed texts. Each sentence in the text has the following format. The first element in each line is the "name" of the line (i.e. "A","B","C","D"). The... (12 Replies)
Discussion started by: John Lyon
12 Replies

3. Hardware

Bluetooth Dongle Pairing but Not Connecting (Linux)

I have been trying to get bluetooth working correctly on Linux for a while now. I am using two systems that are having identical issues: Linux Mint 15 64-bit & Ubuntu 13.04 32-bit. Both are using the Cinnamon desktop. I have the following bluetooth dongle: ... (6 Replies)
Discussion started by: Deluge
6 Replies

4. Programming

Program wont print prime numbers

The problem I'm having is that when you put in the two numbers the answer is just prime.... nothing. I cannot figure this out ive been working on this forever, can someone please god just tell me how to fix this without encrypted "hints". #include <iostream> #include <cmath> using... (3 Replies)
Discussion started by: c++newb
3 Replies

5. Shell Programming and Scripting

Pairing up numbers

Hi, Im trying to script the following logic but having some difficulties and wonder if you can help. Have tried to use "cut" to cut pairs but doesn't appear to do what i need when there are more than one pair. Also "wc -c" doesn't appear to be so easy to know which characters to strip of at I... (3 Replies)
Discussion started by: bunti
3 Replies

6. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

7. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

8. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

9. Shell Programming and Scripting

Linux:Program exit with displaying a print feature

hi All I have a scritp running which connects to a local host and then gets a value from a field and then ftp that value to antoher server. It is running fine, and from crontab it gives the output to a file, the problem is sometime it doesnt run but if i check the output file it does not show one... (0 Replies)
Discussion started by: imran721
0 Replies

10. UNIX for Advanced & Expert Users

Unix Program for data and print

Hi Is there any UNIX program (or Mac OSX) that works with delimitted data files, manipulates the data into a letter / layout form and can then send multiple personalised forms to multiple printers? If this is not the right forum, can anyone point me in the right direction? Thanks Mike Taylor (5 Replies)
Discussion started by: miketaylor
5 Replies

Featured Tech Videos