learningperl01 has asked for the wisdom of the Perl Monks concerning the following question:
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
Second script that sorts based on a pattern match and prints all other lines without changing them.#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
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.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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Merge two scripts into one
by pc88mxer (Vicar) on Feb 15, 2008 at 19:34 UTC | |
|
Re: Merge two scripts into one
by apl (Monsignor) on Feb 16, 2008 at 15:56 UTC |