Hi, 

> As far as your follow up question on the B lines, "only line with a B in
> the beginning in set?," I'm not sure if I understand.  If you mean that
> there will only be 1 line per order (set of lines A-T) with a B in the
> first position, you are correct.

yes, thats what I meant.
Sorry about my lazyness. Adittionally I get to correct all my embarassing 
typos...

> Also, as far as your assumption, "The way I do it assumes that the first
> and only first line of each set beginns with an A (and falsly buts that A
> at the end of the privious record, but
> doesnt matter for the aim her, does it?),"  I'm not sure what you mean by
> this either.  However, it sounds like you have it correct.  Lines that
> indicate the beginning of an order block, will only ever start with an A in
> the first position.

Well, what that $/="\nA" does is, it changes the amount of data the while 
(<FH>) reads into $_
Usually that is a line - in your case, the change of $/ gets it to read a 
whole order into $_: from A,.... to T,..... end of line here. Thats what you 
need. However, I cheat: it acctually reads from A,... to T,.... \nA, into $_, 
so even the (A,) belongs to the next record, it ends up in the privious one. 
Thats kind of wrong, given your record structure but does not matter for the 
purpous you described. See the print $_ in the code below.

> Finally, the final assumption, that "The push assumes that there are always
> exactly 5 records between B and email and that this is the only line with a
> B in record (and comes before the lines
> with ADV_".  I think that this is correct.  

well good:)

> I tested the script, and I was able to output e-mail addresses.  However,
> using the data that I posted, it does not quite output exactly what I need.
> Based on this sample of order.csv and the script that you sent me (I added
> the line "print @email" to view the output):
>
>   for (my $i=0; $i<=$#fields; $i++){
>      if ($fields[$i] eq "B") {$b_index=$i; next;}
>      elsif ($fields[$i] =~ /^ADV_.*/) {push @email, $fields[$b_index+4];
> last;}
1> print @email;
>  ):

>
> What is going wrong?  Am I trying to view the output incorrectly?

The line 1 is still in the for loop. So you print all emails seen so far for 
every field the split gave you.

Code with more debug in the right place:

---------------------------

#! /usr/bin/perl
use strict;
use warnings;

my @email;
open (FH, "<complex.txt") or die "$!";

local $/ = "\nA,"; # make \nA, the record seperator

while(<FH>){       # read the next record
  print "This record holdes:\n$_ \n"; 

  my @fields = split ",|\n", $_; # split at , or \n
  my $b_index; # 0 for every new record
  for (my $i=0; $i<=$#fields; $i++){
     if ($fields[$i] eq "B") {$b_index=$i; next;}
     elsif ($fields[$i] =~ /^ADV_.*/) {push @email, $fields[$b_index+4]; 
last;}
  } # end for

print "End of record.\n\n"
} # end while

print "@email";  #last line in script

-----------------------------

On my box that prints the 2 emails you wanted.
I hope I didnt get something totally screwed.

Let me know if that does it or not. Thx, 
Wolf




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to