#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2

    Trying to assign variables in a STDIN variables


    I'm trying to assign variables that are STDIN variables but I'm having issues.

    I didn't write this script and I'm just now starting to learn perl and reading crash course books on the language, but this is my first speed bump.

    When I assign the variables myself and add it in after the STDIN assignments of each variable, I get errors.
    The perl script asks for user input but I'd like to assign that input within the script so it is done automatically.
    I'd like the variables to be assigned as so:

    Code:
    my $layer = SCR;
    my $startdate = 19400101;
    my $enddate = 19400102;
    my $upperlong = 12194;
    my $lowerlong = 12190;
    my $upperlat = 65227;
    my $lowerlat = 65220;
    my $extract = y;



    Here is the whole original code without my assignments:


    Code:
    #!/usr/local/bin/perl -w
    #
    # Script for removing units outside a specified area and time from the 
    # *.mis files of SH3.
    # Written by LGN1, March 2010
    #
    
    use strict;
    # SCR, RND, or LND layer
    print "SCR, RND, or LND? (s/r/l)";
    my $dlayer = <STDIN>;
    chomp($dlayer);
    my $layer = " ";
    if ($dlayer eq "r") {$layer = "RND"}
    else {$layer = "LND"};
    if ($dlayer eq "s") {$layer = "SCR"};
    open(READF,"Campaign_$layer.mis")
        or die "Error while opening the file: $!\n";
    
    print "Start date (YYYYMMDD) (default 19390101): ";
    my $startdate = <STDIN>;
    if ($startdate eq "\n") {$startdate = 19390101;print "Default: $startdate\n"}
    else {chomp ($startdate)};
    print "End date (YYYYMMDD) (default 19451231): ";
    my $enddate = <STDIN>;
    if ($enddate eq "\n") {$enddate = 19451231;print "Default: $enddate\n"}
    else {chomp ($enddate)};
    print "Upper longitude[km] (default: 5000): ";
    my $upperlong = <STDIN>;
    if ($upperlong eq "\n") {$upperlong = 5000;print "Default: $upperlong\n"}
    else {chomp($upperlong)};
    print "Lower longitude[km] (default: -8000): ";
    my $lowerlong = <STDIN>;
    if ($lowerlong eq "\n") {$lowerlong = -8000;print "Default: $lowerlong\n"}
    else {chomp($lowerlong)};
    print "Upper latitude[km] (default: 10000): ";
    my $upperlat = <STDIN>;
    if ($upperlat eq "\n") {$upperlat = 10000;print "Default: $upperlat\n"}
    else {chomp($upperlat)};
    print "Lower latitude[km] (default: 0): ";
    my $lowerlat = <STDIN>;
    if ($lowerlat eq "\n") {$lowerlat = 0;print "Default: $lowerlat\n"}
    else {chomp($lowerlat)};
    # change from km to meter
    $upperlong = $upperlong * 1000;
    $lowerlong = $lowerlong * 1000;
    $upperlat = $upperlat * 1000;
    $lowerlat = $lowerlat * 1000;
    print "Checking...\n";
    
    # Initialize some variables
    my $deleteunit = 1;
    my $timeframe = 1;
    my $redstring = " ";
    my $counter = 0;
    my $groupcount = 0;
    my $unitcount = 0;
    my $unitcounter = 1;
    my $prevunitcount = 0;
    my $section = "[Group ";
    if ($layer eq "RND") {$section = "[RndGroup "};
    my $nextunit = $section.$unitcounter."]";
    my $prevunit = " ";
    my $stringlength = length($nextunit);
    my @delindx = ();
    my @delgroupindx = ();
    my @delunitindx = ();
    my $unitswitch = "n";
    my $ordswitch = "n";
    my $LongInside = 0;
    my $LatInside = 0;
    my $groupsfound = 0;
    my $unitsfound = 0;
    # Read file and check lines
    while((defined(my $i = <READF>))) {
        $redstring = substr($i,0,$stringlength);
        if ($redstring eq $nextunit) {
    	if (($deleteunit == 0) && ($timeframe == 1)){
    #	    print "$prevunit inside! \n \n";
    	    $counter++}
    	else {push(@delindx,$prevunitcount)};
    #	print "Found $nextunit \n";
    	$prevunitcount = $unitcounter;
    	$unitcounter++;
    	$deleteunit = 1;
    	$timeframe = 1;
    	$prevunit = $nextunit;
    	$nextunit = $section.$unitcounter."]";
    	$stringlength = length($nextunit);
        };
        $redstring = substr($i,0,14);
        if ($redstring eq "GameEntryDate=") {
    	$redstring = substr($i,14);
    	if ($redstring > $enddate) {$timeframe = 0};
        };
        $redstring = substr($i,0,13);
        if ($redstring eq "GameExitDate=") {
    	$redstring = substr($i,13);
    	if ($redstring < $startdate) {$timeframe = 0};
        };
        $redstring = substr($i,0,5);
        if ($redstring eq "Long=") { #print "Longitude found!\n";
    	$redstring = substr($i,5);
    	if (($redstring < $upperlong) && ($redstring > $lowerlong)) {
    	    $LongInside = 1;
    	};
        };
        $redstring = substr($i,0,4);
        if ($redstring eq "Lat=") { #print "Latitude found!\n";
    	$redstring = substr($i,4);
    	if (($redstring < $upperlat) && ($redstring > $lowerlat)) {
    	    $LatInside = 1;
    	};
            if (($LongInside == 1) && ($LatInside == 1)) {
    #	    print "Waypoint inside!\n";
    	    $deleteunit = 0;
    	};
    #        else {print "Waypoint outside!\n" };
            $LongInside = 0;
            $LatInside = 0;
        };
        $redstring = substr($i,0,8);
        if ($redstring eq "[Unit 1]") {
    	if (($deleteunit == 0) && ($timeframe == 1)){
    #	    print "$prevunit inside! \n";
    	    $counter++}
    	else {push(@delindx,$prevunitcount)};
    	$unitswitch = "y";
    	@delgroupindx = @delindx;
    	$groupcount = $counter;
    	$groupsfound = $unitcounter - 1;
    	$counter = 0;
    	@delindx = ();
    #	print "Found [Unit 1]\n";
    	$deleteunit = 1;
    	$timeframe = 1;
    	$prevunitcount = 1;
    	$unitcounter = 2;
    	$prevunit = "[Unit 1]";
    	$nextunit = "[Unit 2]";
    	$section = "[Unit ";
    	$stringlength = length($nextunit);
        };
        $redstring = substr($i,0,16);
        if ($redstring eq "[OrdnanceUnit 1]") {
    	if (($deleteunit == 0) && ($timeframe == 1)){
    #	    print "$prevunit inside! \n";
    	    $counter++}
    	else {push(@delindx,$prevunitcount)};
    	if ($unitswitch eq "n") {
    	    @delgroupindx = @delindx;
    	    @delunitindx = ();
    	    $groupcount = $counter;
    	    $groupsfound = $unitcounter - 1;
    	} else {@delunitindx = @delindx;
    		$unitcount = $counter;
    		$unitsfound = $unitcounter - 1;
    		$counter = 0};
    	@delindx = ();
    #	print "Found [OrdnanceUnit 1]\n";
    	$ordswitch = "y";
    	$deleteunit = 1;
    	$timeframe = 1;
    	$prevunitcount = 1;
    	$unitcounter = 2;
    	$prevunit = "[OrdnanceUnit 1]";
    	$nextunit = "[OrdnanceUnit 2]";
    	$section = "[OrdnanceUnit ";
    	$stringlength = length($nextunit);
        };
    };
    if (($deleteunit == 0) && ($timeframe == 1)){
    #    print "$prevunit inside! \n";
        $counter++}
    else {push(@delindx,$prevunitcount)};
    if ($ordswitch eq "n") {
        if ($unitswitch eq "n") {
    	@delgroupindx = @delindx;
    	@delindx = ();
    	$groupcount = $counter;
    	$groupsfound = $unitcounter - 1;
    	$counter = 0;
        } else {
    	@delunitindx = @delindx;
    	@delindx = ();
    	$unitcount = $counter;
    	$unitsfound = $unitcounter - 1;
    	$counter = 0;
        };
        $unitcounter = 1;
    };
    my $dummy = shift(@delgroupindx);
    my $dummy1 = 0;
    my $dummy2 = "PosTime_Campaign_".$layer.".mis";
    my @delordindx = @delindx;
    my $delNrgroup = @delgroupindx;
    my $delNrunit = @delunitindx;
    my $delNrordn = @delordindx;
    print "Final results: \n";
    print "**************\n";
    print "$groupsfound group(s) found! \n";
    print "$groupcount group(s) inside! \n";
    print "$unitsfound unit(s) found! \n";
    print "$unitcount unit(s) inside! \n";
    $unitcounter--;
    print "$unitcounter ordnance unit(s) found! \n";
    print "$counter ordnance unit(s) inside! \n \n";
    close(READF);
    
    print "Extract units (y/n)? ";
    my $extract = <STDIN>;
    chomp($extract);
    if ($extract eq "y"){
        print "Start extracting...\n";
        open(READF,"Campaign_$layer.mis")
    	or die "Error while opening the file: $!\n";
        open(WRITEF,"> $dummy2")
    	or die "Error while opening the file: $!\n";
        my $oldcount = 1;
        my $newcount = 0;
        my $write = 1;
        my $delcounter = 0;
        $section = "[Group ";
        if ($layer eq "RND") {$section = "[RndGroup "};
        $nextunit = $section.$oldcount."]";
        $prevunit = $nextunit;
        $stringlength = length($nextunit);
        @delindx = @delgroupindx;
        my $maxindx = $delNrgroup;
        my $prevstringlength = $stringlength;
        my $modstring = "";
        my $modstring2 = "";
        while((defined(my $i = <READF>))) {
    	$redstring = substr($i,0,8);
    	if ($redstring eq "[Unit 1]") {
    	    $oldcount = 1;
    	    $newcount = 0;
    	    $section = "[Unit ";
    	    $nextunit = "[Unit 1]";
    	    $stringlength = length($nextunit);
    	    @delindx = @delunitindx;
    	    $maxindx = $delNrunit;
    	    $delcounter = 0;
    	};
    	$redstring = substr($i,0,16);
    	if ($redstring eq "[OrdnanceUnit 1]") {
    	    $oldcount = 1;
    	    $newcount = 0;
    	    $section = "[OrdnanceUnit ";
    	    $nextunit = "[OrdnanceUnit 1]";
    	    $stringlength = length($nextunit);
    	    @delindx = @delordindx;
    	    $maxindx = $delNrordn;
    	    $delcounter = 0;
    	};
    	$redstring = substr($i,0,$stringlength);
    	if ($redstring eq $nextunit) {
    	    if ($delcounter < $maxindx) {
    		if ($oldcount == $delindx[$delcounter]) {
    		    $delcounter++;
    		    $write = 0;
    		    $newcount--;
    		} else {$write = 1};
    	    } else {$write = 1};
    	    $oldcount++;
    	    $newcount++;
    	    $prevstringlength = $stringlength;
    	    $prevunit = $nextunit;
    	    $nextunit = $section.$oldcount."]";
    	    $stringlength = length($nextunit);
    	};
    	$redstring = substr($i,0,14);
    	if ($redstring eq "[EditorParams]") {$write = 1};
    	if ($write == 1) {
    	    $dummy1 = $prevstringlength - 1;
    	    $redstring = substr($i,0,$dummy1);
    	    $dummy = substr($prevunit,0,$dummy1);
    	    if ($redstring eq $dummy) {
    		$modstring = substr($i,$dummy1);
    		$modstring2 = $section.$newcount.$modstring;
    		print WRITEF $modstring2}
    	    else {print WRITEF $i};
    	};
        };
        close(READF);
        close(WRITEF);
    };
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    22
    Rep Power
    0
    Can you post the errors you get?

    Ofcourse if you try assign a static value to it. You should comment out the reading values from the user or STDIN.

    ssharish2005
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by ssharish2005
    Can you post the errors you get?

    Ofcourse if you try assign a static value to it. You should comment out the reading values from the user or STDIN.

    ssharish2005
    Please explain comment out the reading values from the user or STDIN? You mean remove the code that puts in the STDIN?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by ssharish2005
    Can you post the errors you get?

    Ofcourse if you try assign a static value to it. You should comment out the reading values from the user or STDIN.

    ssharish2005
    Please explain comment out the reading values from the user or STDIN? You mean remove the code that puts in the STDIN?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Originally Posted by pditty8811

    When I assign the variables myself and add it in after the STDIN assignments of each variable, I get errors.
    You should declare your variable (with the my function) latest the first time you are using it.

    Here you are using the variables in the part that is reading values from STDIN without declaring them and you declare them an,d refine them afterwards. This does not work. So you should either remove the code that reads froml STDIN, so that, for example,

    Code:
    my $startdate = 19400101;
    is the first time you are using variable $startdate, or move the dclaration of $startdate earlier.

    The other thing is that:

    Code:
    my $layer = SCR;
    and

    Code:
    my $extract = y;
    are wrong because the string value should be between quotes. You should have:

    Code:
    my $layer = "SCR";
    or

    Code:
    my $layer = 'SCR';
    (Same thing for $extract.)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by Laurent_R
    You should declare your variable (with the my function) latest the first time you are using it.

    Here you are using the variables in the part that is reading values from STDIN without declaring them and you declare them an,d refine them afterwards. This does not work. So you should either remove the code that reads froml STDIN, so that, for example,

    Code:
    my $startdate = 19400101;
    is the first time you are using variable $startdate, or move the dclaration of $startdate earlier.

    The other thing is that:

    Code:
    my $layer = SCR;
    and

    Code:
    my $extract = y;
    are wrong because the string value should be between quotes. You should have:

    Code:
    my $layer = "SCR";
    or

    Code:
    my $layer = 'SCR';
    (Same thing for $extract.)
    I've decided not to take this approach as it would be impractical for what I'm trying to achieve. I will be releasing a mod to a game, and using this method would require everyone to install perl in order to use the mod. I'd like to mek the perl into an exe then release my mod, that way it will be more userfriendly and less of a hassle for others.

    Please ignore this thread.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    OK, thank you very much for your very grateful thanks. I appreciate that.

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by Laurent_R
    OK, thank you very much for your very grateful thanks. I appreciate that.

    Thank you!!!!!!!

IMN logo majestic logo threadwatch logo seochat tools logo