learningperl01 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone I wondering if someone could give me some pointers/help with how to go about combining the following two scripts into one. I have a script that generates lines of text and a second script which will sort a file based on a unique pattern matched by Regex expression. First script that generates/creates ID and other information. Like shown below.

id: 4949 data: 001 identifier: xxx-yyy
id: 5900 data: 039 identified: zzz-aaa

Here is the first script.
if ($ARGV[0] eq "new") { @hex = system("/home/testuser01/new.php $ARGV[0] > $path$r_file"); open(TEMP, "$path$r_file") || die "Count not open resource file"; open(ORIGINAL_GEN, "$path$old_id") || die "Count not open old ID file +"; @gen_array1 = <ORIGINAL_GEN>; open(NEW_GEN, ">/home/testuser01/new-gen.txt") || die "Could not open + new generator file"; foreach $line(@gen_array1) { if($line =~ /id:\s?(\d+)/) { print $line, "\n"; chomp $line; if($line =~ /id:\s?(\d+)//) { $line =~ $line =~ s/.*id:\s?(\d+).*//sgi; print NEW_GEN $line, "\n"; } else { print NEW_GEN $line, "\n"; while (<TEMP>) { chomp; next if /^\s*$/; $id++; $num++; @array1 = split; print NEW_GEN "id:$id" , "data:\"$count\"\; " , "identifier:", "\"|@ +array1\"\; " , "\n"; } } } } } close($ORIGINAL_GEN); close($NEW_GEN); close($TEMP); [/code The second script does the following Reads in a file line by line, each line is being placed into an array. + A sample file contains lines of text as shown below unsort with comm +ents etc.... <code> #Comment *ss id addr id: 099 bbb ccc aaa *ss id addr id: 003 aaa bbb ccc #Comment 2 *333 23 ss id: 002 aaa bbb ccc *22 233333333 34432 233 44

What I am trying to do is sort the lines by the pattern "/id\:\s?(0-9)" and if a line does not contain the "id: number" then just print the line as it is in the file. so the new output file would contain the following: They are being sorted by the ID: field followed by the number. Final output after sorting looks like below.
#Comment *333 23 ss id: 002 aaa bbb ccc *ss id addr id: 003 aaa bbb ccc #Comment 2 *ss id addr id: 099 bbb ccc aaa *22 233333333 34432 233 44
Second script that sorts based on a pattern match and prints all other lines without changing them.
unlink("$path$rfile"); open(FILE, "/home/testuser01/") || die "Count not open old Gen_file"; @lines = <FILE>; @matching_indices = grep $lines[$_] =~ /id\:\s?\d/, 0 .. $#lines; @matching_lines = @lines[@matching_indices]; @sorted_matching_lines = sort { my ($an) = ( $a =~ /id\:\s?(\d+)/ ); my ($bn) = ( $b =~ /id\:\s?(\d+)/ ) +; $an <=> $bn } @matching_lines; @lines[@matching_indices] = @sorted_matching_lines; print @lines;
So what I would like to do is add the sorting portion of the second script to the first, that way everything would be under one script. I am at this point stuck since what I have tried does not seem to be working. So any ideas on how to append the sort porting of the code to the first script would really help me out as I think I have been looking at this so long now that I am just confusing myself. thanks for all help in advance.

Replies are listed 'Best First'.
Re: Merge two scripts into one
by pc88mxer (Vicar) on Feb 15, 2008 at 19:34 UTC
    First, let me say that you have very peculiar kind of merge. At first I didn't completely understand what you were trying to do.

    Instead of emitting the results of the first script to a file, you need to save them in a perl data structure. An AOH (array of hashes) would probably work well here. Also, I'd keep track of the comment lines as you generate them in the first script.

    In your first script, instead of printing a comment line to NEW_GEN, do this:

    $comment{$lineno++} = $_;

    And instead of printing an id line to NEW_GEN, do this:

    push(@lines, { id => $id, line => $_ }); $lineno++;

    Then the second script becomes:

    @lines = sort { $a->{id} <=> $b->{id} } @lines; for (0..$lineno-1) { if (exists $comment{$_}) { print $comment{$_}; } else { my $x = shift(@lines); print $x->{line}; } } # @lines should be empty at this point

    This will keep the comment lines in their original places while sorting the id lines.

Re: Merge two scripts into one
by apl (Monsignor) on Feb 16, 2008 at 15:56 UTC
    I am at this point stuck since what I have tried does not seem to be working.

    It's a shame you didn't just show us the merged script (rather than the two separate ones). We might have been able to help you find the problem.