Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combining two bits of code 1

Status
Not open for further replies.

woodymo

MIS
Feb 19, 2005
3
0
0
US
I am somewhat new to Perl, but I have worked with batch files for a long time. I have two programs that I usually call from a batch file. The first sorts data and the second extracts the unique items. I would like to combine these into one script, but I am getting errors when the second OPEN INFILE line is read. I believe this is due to the fact that the first process has not had time to finish the text file that the second one needs. I have attempted to use the sleep and wait lines but have had no success. I have tried to close APPEND; but that did not work either. Can anyone show me what I am doing wrong? Here is what I have. Thank you.


#this file will sort items from a text file

open(INFILE,"print_job_output.txt") || die "Can't open: $!\n";
open(APPEND, ">>sorted_data.txt");

my @words=<INFILE>;

foreach(sort mysort @words) {
print APPEND;
}

sub mysort {

($a) cmp ($b);

}

close(INFILE)


####################################################
##extract the unique ones now
####################################################

open(INFILE,"sorted_data.txt") || die "Can't open: $!\n";
open(APPEND, ">>unique_data.txt");
while($line=<INFILE>){
@line=<INFILE>;

$prev = "not equal to $line[0]";
@out = grep($_ ne $prev && ($prev = $_, 1), @line);

print APPEND @out;
}
close(INFILE)
 
Hi woody,

You're a bit mixed up in the second bit I think, try something like this; and yes, I think you should close APPEND before the second bit.

Code:
open(SORTED,"sorted_data.txt") 
[tab]|| die "Can't open sorted: $!\n";
open(UNIQUE,">unique_data.txt") 
[tab]|| die "Can't create unique: $!\n";

$prev='';
while(<SORTED>){
[tab]print UNIQUE $_ unless $_ eq $prev;
[tab]$_ = $prev;
}
close SORTED;
close UNIQUE;



Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Thanks, but that won't work because neither sorted_data.txt or unique_data.txt exist when the program starts. The file print_job_output.txt does exist before the program runs.

When each program runs alone (ie: split it where it says ##extract the unique one now) they run fine.
 
the code I posted was a replacement for your second section - it was not a complete solution

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
woodymo, do you require sorted_data.txt for any reason other than generating the unique elements? If not, you can bypass writing the temporary file with something like this:

Code:
my %words;
open(INFILE, "<print_job_output.txt") || die "Can't open print_job_output.txt: $!\n";
while (<INFILE>) {
    chomp;
    $words{$_}++;
}
close INFILE;

open(UNIQUE, ">>unique_data.txt") || die "Can't open unique_data.txt: $!\n";
foreach (sort keys %words) {    # $a cmp $b is the default sort
    print UNIQUE "$_\n";
}
close UNIQUE;
 
Woody

It's simpler to use a hash. Because a hash can only consist of unique values, you can use it to automatically de-dupe your data set without sorting it.
Code:
#!/usr/bin/perl
use warnings;
use strict;

my %uniq;

while (<DATA>) {
    chomp;
    $uniq{$_}++;
}

print "$_\n" foreach [red]sort[/red] keys %uniq;

__DATA__
one
two
two
three
three
three
But if you want the output file sorted, add the 'sort' keyword shown in red.
 
Thank you all for your solutions. I think rharsh's will work best for what I am doing now. I think my next step in learning Perl will be to discover how these hashes work. Thank you all again.
 
hi woodymo

as stevexff explains - hashes are named so they can only have one value

Code:
#!/usr/bin/perl

chomp (@lines = <DATA>);

foreach $line (@lines) {
  $hash{$line} = 1;
}

while (($key, $value) = each %hash) {
  print "$key\n";
}

__DATA__
one
one
one
two
two
two
two
three
three
four
four
four
four
four
five
five
five

will output...

one
two
three
four
five

(these will be in a random order due to the inner workings of Perl)


Kind Regards
Duncan
 
if you are on a *NIX system you can't do from the command line?

sort print_job_output.txt | uniq


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top