in reply to Re: Multiple command prints in same output file
in thread Multiple command prints in same output file

Here is the complete piece of code:

#!/usr/bin/perl -w $DescribeFile = $ARGV[0]; $ComponentFile = $ARGV[1]; $LoadCreateFile = ">".$ARGV[2]; # *** Process the data from the input file *** $Pause .= "pause\n"; &Main_Process; # *** Subroutine that does the main processing of program. *** sub Main_Process { $IntFound = 0; $RootFound = 0; &OpenProjectFile; foreach $_ (@DescribeData) { &ProcessGetViewInfo; } close LOADCREATE; } # *** Subroutine that Opens, reads and closes the input file * +** sub OpenProjectFile { open DESCRIBE, $DescribeFile or die "Can not find the project descri +be file!"; while (!eof(DESCRIBE)) { read DESCRIBE, $DescribeDataFromFile, 100; $DescribeData .= $DescribeDataFromFile; } close DESCRIBE; $DescribeData =~ s/\n\n/\n/g; @DescribeData = split("\n", $DescribeData); } # *** Subroutine that gets the required view info from the file + *** sub ProcessGetViewInfo { &FindTag; } # *** Subroutine that finds and stores the view tag name for the curen +t view *** sub FindTag { if (m/project "/) { $ProjectLine = $_; $PrjLeftQuote = index($ProjectLine, "t"); $NameLength = length($ProjectLine) - $PrjLeftQuote - 4; $ProjectName = substr($ProjectLine, $PrjLeftQuote + 3, $NameLength +); } if (m/integration stream:/) { $DescribeLine = $_; $LeftQuote = index($DescribeLine, ":"); $RightQuote = index($DescribeLine, "@"); $Intname = $RightQuote - $LeftQuote - 2; $Int = substr($DescribeLine, $LeftQuote + 2 , $Intname); $Int2 = substr($DescribeLine, $LeftQuote + 2 , length($DescribeLin +e)); $CreateViewCmd .= "cleartool mkview -snapshot -tag ".$Int."_Snapsh +ot -stream ".$Int2." -colocated_server -host idetestcc1 -hpath d:\\Cl +earCase_Storage\\views\\snapshot\\".$Int."_Snapshot -gpath d:\\ClearC +ase_Storage\\views\\snapshot\\".$Int."_Snapshot \\\\idetestcc1\\ccstg +_d\\views\\snapshot\\".$Int."_Snapshot\n"; $IntFound ++; &OpenComponentFile; foreach $_ (@ComponentData) { &ProcessComponent; } } } # *** Subroutine that Opens, reads and closes the input file * +** sub OpenComponentFile { open COMPONENT, $ComponentFile or die "Can not find the component fi +le!"; while (!eof(COMPONENT)) { read COMPONENT, $CompDataFromFile, 100; $ComponentData .= $CompDataFromFile; } close COMPONENT; $ComponentData =~ s/\n\n/\n/g; @ComponentData = split("\n", $ComponentData); } # *** Subroutine that gets the required view info from the file + *** sub ProcessComponent { &FindRoot; if ($RootFound == 1) { &CreateLoadFile; } } # *** Subroutine that finds and stores the root name for the current p +roject *** sub FindRoot { if (m/$ProjectName/) { if (m/root directory:/) { $RootLine = $_; $LeftQuoteRoot = index($RootLine, ":"); $RightQuoteRoot = length($RootLine) - $LeftQuoteRoot - 4; $Root = substr($RootLine, $LeftQuoteRoot + 3, $RightQuoteRoot); $LoadViewCmd .= "cleartool update -add_loadrules \.".$Root." \n +"; $D .= "d:\n"; $ChangeDirectory .= "cd clearcase_storage\\views\\snapshot\\".$ +Int."_Snapshot\n"; $RootFound ++; } } } # *** Subroutine that opens and prints the load spec file +*** sub CreateLoadFile { open LOADCREATE, $LoadCreateFile or die "Can not find the LoadCreate +File!"; print LOADCREATE $CreateViewCmd; print LOADCREATE $D; print LOADCREATE $ChangeDirectory; print LOADCREATE $LoadViewCmd; print LOADCREATE $Pause; $RootFound = 0; }

Hope this helps!

Replies are listed 'Best First'.
Re^3: Multiple command prints in same output file
by davidrw (Prior) on Jul 08, 2005 at 13:00 UTC
    first comment is use strict (among other things this will complain about the undeclared global variable $RootFound) .. after that, I'm confused as to why $LoadCreateFile is opened for writing and _clobbered_ each time.. that will result in just 1 set of print's ever being in there..

    (i meant to ask earlier) could you post a sample of the output you're currently getting and a sample of the desired output?
      This is what I expect:

      cleartool mkview -snapshot -tag ASTest_Project_Integration_Snapshot -s +tream ASTest_Project_Integration@\PVOB_Test -colocated_server -host i +detestcc1 -hpath d:\ClearCase_Storage\views\snapshot\ASTest_Project_I +ntegration_Snapshot -gpath d:\ClearCase_Storage\views\snapshot\ASTest +_Project_Integration_Snapshot \\idetestcc1\ccstg_d\views\snapshot\AST +est_Project_Integration_Snapshot cleartool mkview -snapshot -tag Test_Johan_Integration_Snapshot -strea +m Test_Johan_Integration@\PVOB_Test -colocated_server -host idetestcc +1 -hpath d:\ClearCase_Storage\views\snapshot\Test_Johan_Integration_S +napshot -gpath d:\ClearCase_Storage\views\snapshot\Test_Johan_Integra +tion_Snapshot \\idetestcc1\ccstg_d\views\snapshot\Test_Johan_Integrat +ion_Snapshot cleartool mkview -snapshot -tag 1000_test_snapshot_Int_Snapshot -strea +m 1000_test_snapshot_Int@\PVOB_Test -colocated_server -host idetestcc +1 -hpath d:\ClearCase_Storage\views\snapshot\1000_test_snapshot_Int_S +napshot -gpath d:\ClearCase_Storage\views\snapshot\1000_test_snapshot +_Int_Snapshot \\idetestcc1\ccstg_d\views\snapshot\1000_test_snapshot_ +Int_Snapshot d: d: d: cd clearcase_storage\views\snapshot\1000_test_snapshot_Int_Snapshot cd clearcase_storage\views\snapshot\1000_test_snapshot_Int_Snapshot cd clearcase_storage\views\snapshot\1000_test_snapshot_Int_Snapshot cleartool update -add_loadrules .\InnovComp2\PRJ_1000_test_snapshot cleartool update -add_loadrules .\InnovComp2\PRJ_1000_test_snapshot cleartool update -add_loadrules .\InnovComp2\PRJ_1000_test_snapshot

      This is what I would like to see:

      cleartool mkview -snapshot -tag ASTest_Project_Integration_Snapshot -s +tream ASTest_Project_Integration@\PVOB_Test -colocated_server -host i +detestcc1 -hpath d:\ClearCase_Storage\views\snapshot\ASTest_Project_I +ntegration_Snapshot -gpath d:\ClearCase_Storage\views\snapshot\ASTest +_Project_Integration_Snapshot \\idetestcc1\ccstg_d\views\snapshot\AST +est_Project_Integration_Snapshot d: cd clearcase_storage\views\snapshot\1000_test_snapshot_Int_Snapshot cleartool update -add_loadrules .\InnovComp2\PRJ_1000_test_snapshot
      This last section must be done for each input record, so the detail will be different.
        ok.. i get it now .. so what you're doing is looping through and for each record your tacking via string concatenation stuff onto several different variables ( $CreateViewCmd, D, ChangeDirectory, LoadViewCmd, Pause ). Then just printing those "once" (well, multiple times but file gets clobbered each time) at the end -- that's why the "like" commands are grouped together.

        so .. how to go about fixing this -- basic approach is that the order needs to be maintained .. e.g. the LoadViewCmd stuff for row 3 has to come before the ChangeDirectory stuff for row 2.

        One way (quickest to code) to this would be to print LOADCREATE $somestring as you go..

        Another way would be to save those variables for each datarow so they stay together w/in the datarow.
        my @rows; foreach (){ # for a data row my %row = ( map { $_ => '' } qw/CreateView Cmd D ChangeDirectory Loa +dView Pause/ ); # do your stuff, but change stuff like: $Pause .= "foo\n"; # to: $row{Pause} .= "foo\n"; # --> note scoping issues .. you probably want to pass around a refe +rence to %row push @rows, \%row; } # and not print everything at the end... open LOADCREATE, ...; foreach my $row (@rows){ print LOADCREATE $row-{CreateViewCmd}; print LOADCREATE $row->{D}; print LOADCREATE $row->{ChangeDirectory}; print LOADCREATE $row->{LoadViewCmd}; print LOADCREATE ${Pause}; } close LOADCREATE;