Loading ...
Sorry, an error occurred while loading the content.
 

Re: Split and Match

Expand Messages
  • techybill
    Hi After playing with this I rewrote it as follows, it works but takes a long time. Just wondering if there is another way to write something like this but
    Message 1 of 4 , Apr 1, 2003
      Hi
      After playing with this I rewrote it as follows, it works but takes
      a long time. Just wondering if there is another way to write
      something like this but executes much faster.

      #!/usr/bin/perl -w
      use CGI::Carp qw/fatalsToBrowser/;
      print "Content-type: text/html\n\n";
      $datafile_news= "./news.csv";
      $datafile_quest = "./guestbookphp.csv";
      open (MATCHES , ">./match.txt")|| die "Can't open matches: $!\n";
      open (CUSTOMER , $datafile_news) || die "Can't open customer.txt: $!
      \n";
      chomp (my @cust = <CUSTOMER>);
      @cust = sort @cust;
      close(CUSTOMER);
      my $cust_product;
      foreach $cust_product (@cust){
      open(GUEST,"$datafile_quest")|| die "Can't Open $datafile_quest";
      while ($guest = <GUEST>)
      {
      chop($guest);
      @row1 = split(/\|/,$guest);
      if ($cust_product eq $row1[2]) {
      print MATCHES "$cust_product|$row1[1] \n";
      close(GUEST);
      }
      }
      }
      close(MATCHES);



      --- In perl-beginner@yahoogroups.com, "techybill" <billbaiden@h...>
      wrote:
      > Hi
      >
      > Trying to split two flat files and match results and rewrite
      > one new file, here is small copy of the format of the files.
      >
      > File #1
      > SFDSF@e...
      > RTR@p...
      > YT5@c...
      > JTDF@m...
      > GCEER@w...
      > KTW@b...
      > MFT@v...
      > YE@t...
      >
      > File #2
      > 40|Debra Jantz|SFDSF@e...|||friend|0
      > 37|Lisa Tatt|trea@m...|||friend|0
      > 35|lil|MFT@v...|||friend|0
      > 38|charlene barl|tist@a...|||friend|0
      > 39|Carol Gode|YE@t...|||friend|0
      >
      > Here is what I've been trying
      > #!/usr/bin/perl -w
      >
      > use CGI::Carp qw/fatalsToBrowser/;
      > print "Content-type: text/html\n\n";
      > $datafile_1= "./guestbookphp.csv";
      >
      > $datafile_2 = "./news.csv";
      >
      > $new_file = "./new_file.txt";
      >
      > open(MAILER,"$datafile_1")|| die "Can't Open $datafile_1";
      >
      > open(GUEST, "$datafile_2") || die "Can't Open $datafile_2";
      >
      > while ($mail = <MAILER>)
      > {
      > chop($mail);
      > @row1 = split(/\|/,$mail);
      > }
      > while ($guest = <GUEST>)
      > {
      > chop($guest);
      > @row2 = split(/\|/,$guest);
      >
      > if ($row1[0] eq $row2[2])
      >
      > {
      >
      > print "$row1[1]|$row1[2]<br>";
      > }
      > }
      >
      > Any idea's would be great. The finished file would be in the
      format
      > of Name|Email if email macthed in both files. I also tried to put
      > the first file into a hash and them let it read through but it did
      > not work either.
      >
      > Thanks Bill
      > Automotive Tech leaning perl
    • Damien Carbery
      ... takes ... The algorithm I used was: 1) Read guestbookphp.csv file (one with email addresses) into a hash. 2) Read in news.csv, parse out email address
      Message 2 of 4 , Apr 1, 2003
        --- In perl-beginner@yahoogroups.com, "techybill" <billbaiden@h...>
        wrote:
        > Hi
        > After playing with this I rewrote it as follows, it works but
        takes
        > a long time. Just wondering if there is another way to write
        > something like this but executes much faster.
        >
        > #!/usr/bin/perl -w
        > use CGI::Carp qw/fatalsToBrowser/;
        > print "Content-type: text/html\n\n";
        > $datafile_news= "./news.csv";
        > $datafile_quest = "./guestbookphp.csv";
        > open (MATCHES , ">./match.txt")|| die "Can't open matches: $!\n";
        > open (CUSTOMER , $datafile_news) || die "Can't open customer.txt: $!
        > \n";
        > chomp (my @cust = <CUSTOMER>);
        > @cust = sort @cust;
        > close(CUSTOMER);
        > my $cust_product;
        > foreach $cust_product (@cust){
        > open(GUEST,"$datafile_quest")|| die "Can't Open $datafile_quest";
        > while ($guest = <GUEST>)
        > {
        > chop($guest);
        > @row1 = split(/\|/,$guest);
        > if ($cust_product eq $row1[2]) {
        > print MATCHES "$cust_product|$row1[1] \n";
        > close(GUEST);
        > }
        > }
        > }
        > close(MATCHES);
        >
        >
        > <snip original message>

        The 'algorithm' I used was:
        1) Read guestbookphp.csv file (one with email addresses) into a hash.
        2) Read in news.csv, parse out email address and if in hash, print
        name and email to output file.

        Using a hash means an immediate (and therefore fast) lookup to
        determine if the email address was in guestbookphp.csv.

        Using this code (and the data from your first mail), matches.txt
        contains:
        Debra Jantz|SFDSF@e...
        lil|MFT@v...
        Carol Gode|YE@t...

        The code:
        #!/usr/bin/perl -w

        use strict;
        use CGI::Carp qw/fatalsToBrowser/;

        print "Content-type: text/html\n\n";
        my $datafile_news= "./news.csv";
        my $datafile_quest = "./guestbookphp.csv";

        # Read in email addresses and put into a hash.
        open( EMAILADDRS , $datafile_quest) or die "Can't open
        $datafile_quest: $!\n";
        my %EmailAddrs;
        while ( <EMAILADDRS> )
        {
        chomp;
        $EmailAddrs{ $_ } = '.';
        }
        close(EMAILADDRS);


        open( CUST_DETAILS,"$datafile_news" ) or die "Can't Open
        $datafile_news";
        open( MATCHES , ">./match.txt" ) or die "Can't open matches: $!\n";
        while ( <CUST_DETAILS> )
        {
        chomp;
        my @splitline = split( /\|/, $_ );
        # If the email address is in $datafile_quest print name
        # and address to file.
        if ( defined $EmailAddrs{ $splitline[2] } )
        {
        print MATCHES "$splitline[1]|$splitline[2]\n";
        }
        }
        close(CUST_DETAILS);
        close(MATCHES);
        __END__
      • Frankie
        Hi guys.. I have passed a subroutine containing sql statement to a session variable.. and I need to run that sub stored in the var name.. for example: sub
        Message 3 of 4 , Apr 1, 2003
          Hi guys..

          I have passed a subroutine containing sql statement to a session variable..

          and I need to run that sub stored in the var name..

          for example:

          sub mysqlcode
          {
          #sql query would be here..
          }

          my $csv_query = mysqlcode;

          $query = $csv_query($session_user_client);

          it doesn't even come close to working...


          anyone know how I could go about doing this??

          any help is much appreciated.


          rgds

          Franki
        Your message has been successfully submitted and would be delivered to recipients shortly.