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

20760Re: [PBML] Searching 3 files?

Expand Messages
  • Jeff
    Mar 31, 2005
      WOW!!

      I was trying to get something done but was at a loss. This is great!
      Funny... i'm sure by the time I would have figured something out, it
      would have been twice as long.. and twice as slow!! haha

      Thanks so much!!!


      --- In perl-beginner@yahoogroups.com, Offer Kaye <offer.kaye@g...> wrote:
      > On Thu, 31 Mar 2005 21:20:38 -0000, Jeff wrote:
      > >
      > > Basically, open the file Libr_Vols.csv, then open Libr_Scr.csv and
      > > TSM_Usage.csv. If the value exists (for example 400002), then do
      > > nothing. If it can't be found in either of the two files (Libr_Scr or
      > > TSM_Usage) then throw it out to a log file.
      > >
      >
      > Here's one way. There are many others :-)
      > ##################### begin code
      > my %seen;
      > update_seen("Libr_Scr.csv" );
      > update_seen("TSM_Usage.csv");
      >
      > my $infile = "Libr_Vols.csv";
      > open(IN,"$infile") or die "Couldn't open $infile for reading: $!\n";
      > while(<IN>) {
      > my $num = (split/,/)[0];
      > if (! defined $seen{$num}) {
      > print "$num\n";
      > }
      > }
      > close(IN) or die "Couldn't close $infile after reading: $!\n";
      >
      > sub update_seen {
      > my $infile = shift;
      > open(IN,"$infile") or die "Couldn't open $infile for reading: $!\n";
      > while(<IN>) {
      > ++$seen{(split/,/)[0]};
      > }
      > close(IN) or die "Couldn't close $infile after reading: $!\n";
      > }
      > ##################### end code
      >
      > Explanations:
      > * %seen is first populated with the numbers from the 2 files you want
      > to check against the master (the keys hold the numbers, the value is
      > not important, as long as it's defined). This is done by the
      > update_seen() sub.
      > * Now the master is checked. If for any number in the master file,
      > there does not exist a defined value in the %seen hash, then that
      > number was not found in either of the two files, and is printed to
      > STDOUT. If it is defined, nothing is done.
      > * (split/,/)[0] splits the current value of $_ (which is the current
      > line) on a comma, and returns just the first member of the array that
      > split returns - the number, in the case of the files here.
      >
      > If anything is unclear, please ask.
      > Hope this helps,
      > --
      > Offer Kaye
    • Show all 6 messages in this topic