Thread: Variable going out of scope

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    28

    Variable going out of scope

    While trying to generate some data from some log files it seems like the variable 'count', as can be seen the code below never prints anything. There some few other problems mostly with variables. For instance, 'previous_date' never prints anything while the new line immediately after it does print. Could anything have a clue what is happening here?

    Thought someone might find this interesting and even suggest possible solutions.

    It has not been possible for me to produce a smaller example that demonstrates this issue but for someone who is familiar with Perl, it would probably be easy to pick out the issue.

    Code:
    use strict;
    use warnings;
    
    sub GenerateData() {
        my $file = 'patch_log.txt';
        my $count = 0;
        my $date = "";
        my $previous_date = "";
        my $patch_file = 'patch_file.txt';
        open my $overview, $patch_file or die "Could not open $patch_file: $!";
        open my $info, $file or die "Could not open $file: $!";
        while (my $line = <$info>) {
               if ($line =~ /^changeset/)
               {
                $count = $count + 1;
                while ( $line = <$info>) {
                    if ($line =~ /^user/)
                    {
                        my $author = substr($line, 10);
                        
                        print $author;
                    }
    
                    if ($line =~ /^date/)
                    {
                        print $date;
                        $previous_date = $date;
                        print $previous_date;
    
                        my $date = substr($line, 10, 14);
                        
                        print $previous_date;
                        #print $date;
                        #print "\n";
                    }
                }
    
               print $count;
            }
    
            else
            
            {
                continue;
            }
    
            last if $. == 20000;
        }
    
        close $info;
    }
    
    GenerateData()

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,709
    First thing, clean up your indentation.
    Code:
    use strict;
    use warnings;
    
    sub GenerateData() {
        my $file = 'patch_log.txt';
        my $count = 0;
        my $date = "";
        my $previous_date = "";
        my $patch_file = 'patch_file.txt';
        open my $overview, $patch_file or die "Could not open $patch_file: $!";
        open my $info, $file or die "Could not open $file: $!";
        while (my $line = <$info>)
        {
          if ($line =~ /^changeset/)
          {
            $count = $count + 1;
            while ( $line = <$info>)
            {
              if ($line =~ /^user/)
              {
                my $author = substr($line, 10);
                print $author;
              }
              if ($line =~ /^date/)
              {
                print $date;
                $previous_date = $date;
                print $previous_date;
    
                my $date = substr($line, 10, 14);
    
                print $previous_date;
                #print $date;
                #print "\n";
              }
            }
            print $count;
          }
          else
          {
            continue;
          }
          last if $. == 20000;
        }
    
        close $info;
    }
    
    GenerateData()
    Second thing, a few lines of representative data with lines which match each of your regex's would go a long way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    28
    Quote Originally Posted by Salem View Post

    Second thing, a few lines of representative data with lines which match each of your regex's would go a long way.
    Uploading the repos to Github might get me the matching lines ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variable scope in C !!
    By thebenman in forum C Programming
    Replies: 17
    Last Post: 10-25-2014, 08:43 AM
  2. scope of a variable
    By Satya in forum C Programming
    Replies: 3
    Last Post: 05-21-2014, 07:54 AM
  3. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  4. Variable Scope
    By Matty_Alan in forum C Programming
    Replies: 3
    Last Post: 11-23-2008, 03:28 PM
  5. Variable Scope
    By tinkerbell20 in forum C++ Programming
    Replies: 5
    Last Post: 06-22-2005, 10:31 PM

Tags for this Thread