Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a rather strange way to look at the data, but I think I got it to work the way you tried. It would be interesting to see why you want the data to be that way. Maybe provide column headings next time. Knowing why you do something in a certain way often makes it a lot easier to think of ways to achive it imo.</p> <p>So here's what I did. Don't get confused, I put your values from file A and file B into scalars and changed the part about reading them.</p> <pre><code>my $file_a = qq~Fiona\tNicole Sherry James\tAlan Nicole Michelle\tCrystal Racheal\tBobby Dan Nicole ~; my $file_b = qq~Fiona\tRacheal,Jack Michelle\tRacheal Racheal\tFiona,Michelle Jack\tFiona ~; </code></pre> <p>After that, proceed to read the 'files'.</p> <pre><code># 1: Store file A in a hash my (%file_a); foreach my $a (split /\n/, $file_a) { my @temp = split /\t/, $a; $file_a{$temp[0]} = $temp[1]; } # 2: Go through file B foreach my $b (split /\n/, $file_b) { my @line_b = split /\t/, $b; # Look in stored file A if the teacher is there if (exists $file_a{$line_b[0]}) { my (%new_hash_table, @teachers); # Put all the students of this teacher into a new hash $new_hash_table{$_} = '' foreach split / /, $file_a{$line_b[0]}; # 3: Take one of the group of teachers who are grouped with the # current teacher at a time foreach my $teacher (split /,/, $line_b[1]) { if (exists $file_a{$teacher}) { # 4: This teacher from the group has students listen in file A push @teachers, $teacher; # Store the teacher's name for print later foreach (keys %new_hash_table) { # 5: Fill the students as csv for the student keys from step 2 $new_hash_table{$_} = join(',', split(/ /, $file_a{$teacher})); } } } foreach my $student (keys %new_hash_table) { # 6: Print... print join("\t", # Student-student relation $student, $new_hash_table{$student}, # Teacher-teacher relation $line_b[0], @teachers); print "\n"; } } } </code></pre> <p>For me that provides the following output:</p> <pre><code>Sherry Bobby,Dan,Nicole Fiona Racheal Nicole Bobby,Dan,Nicole Fiona Racheal Crystal Bobby,Dan,Nicole Michelle Racheal Bobby Crystal Racheal Fiona Michelle Nicole Crystal Racheal Fiona Michelle Dan Crystal Racheal Fiona Michelle </code></pre> <p>This is probably weird since I don't have all the values.</p> <p>Anyways, there are a few things to be said to this. </p> <p>In your example code you used a regex like <code>$data=~/^(\S+)\s+(.*)$/;</code> to get to the values of a simple two-column list. It is a lot easier to use the <a href="http://perldoc.perl.org/functions/split.html" rel="nofollow">split operator</a> to do that.</p> <p>When you read from a file with the <code>&lt;FILEHANDLE&gt;</code> syntax, you can put the scalar you want your lines to go into in the <code>while</code> loop's condition like so:</p> <pre><code>while (my $data = &lt;GROUP&gt;) { chomp $data </code></pre> <p>Also it is <a href="http://docstore.mik.ua/orelly/perl4/lperl/ch11_01.htm" rel="nofollow">common</a> to write filehandle names in all-caps.</p> <p>I'd suggest you take a look at the 'Learning Perl'. The basic concepts of hashes and arrays in there should be enough to takle tasks like this one. Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload