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.

In reply to Merge two scripts into one by learningperl01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.