-8
use Text::Diff;
my $count;
our $stats2 = 0;
for($count = 0; $count <= 1000; $count++){
    my $data_dir="archive/oswiostat/oracleapps.*dat";
    my $data_file= `ls -t $data_dir | head -1`;
    chomp($data_file);
    while(defined($data_file)){
        print $data_file;
        open (DAT,$data_file) || die("Could not open file! $!");
        my @stats1 = stat $data_file;
        @raw_data=<DAT>;
        close(DAT);
        print "Stats1 is :$stats1[9]\n";
        sleep(5);
        print "Checking $stats1[9] equals $stats2\n";
        chomp $stats[9];
        chomp $stats2;
        if($stats1[9] != $stats2){
            print "I am here";
            my @diff = diff \@raw_data, \@raw_data2, { STYLE => "Context" };
            @trim_diff = removedash(@diff);
            print "@trim_diff";
        }
        @raw_data2=@raw_data;
        $stats2 = $stats1[9];
        print "Stat2: $stats2\n";
    }
}

sub removedash {
    print "Its in sub";
    my $cnt=0;
    foreach (@_) {
        chomp;
        if (/^- /){
            s#^- ##g;
            $ar[$cnt] = "$_\n";
            $cnt++;
        }
    }
    return (@ar);
}   

I am using a sub to remove dashes from array and send it back. The @trim_diff should have the return @ar, but it is showing nothing. I am not sure where i have done wrong. Can you guys help me with it?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
VeerM
  • 105
  • 4
  • 11
  • 2
    There's this little thing called "indentation". You should look into it. – Paul Tomblin Jun 29 '12 at 00:12
  • 3
    You return `@ar`, but you never declare it. You should `use strict;`. – Paul Tomblin Jun 29 '12 at 00:13
  • The return statement should work (BTW the parentheses are unnecessary). It might help if we knew what data your program is reading. – Keith Thompson Jun 29 '12 at 00:18
  • @KeithThompson see http://stackoverflow.com/questions/11251666/in-perl-how-can-i-watch-a-directory-for-changes – Sinan Ünür Jun 29 '12 at 00:19
  • Are you sure your `if (/^- /)` is true for at least one row? – Dondi Michael Stroma Jun 29 '12 at 00:20
  • @SinanÜnür: How is array return "fatally flawed"? If http://stackoverflow.com/questions/11251666/in-perl-how-can-i-watch-a-directory-for-changes supposed to answer that, I'm missing something. – Keith Thompson Jun 29 '12 at 00:26
  • @KeithThompson I was being sarcastic. The title of the question is *Array return is not working*. The OP's issue seems to stem from the `if` which prevents array elements from being assigned if they don't start with `'- '`. – Sinan Ünür Jun 29 '12 at 00:27
  • @SinanÜnür: Sarcasm isn't always clear in print. If I didn't pick up on it, I doubt that the OP would have. (Admittedly I didn't read your comment as carefully as I should have.) – Keith Thompson Jun 29 '12 at 00:29

1 Answers1

2

I doubt it's not working. It's probably returning an empty list.

Your function

removedash

would be more aptly named

remove_elements_without_dashes_and_remove_dashes_from_those_ones.

Dondi Michael Stroma
  • 4,668
  • 18
  • 21
  • If i do it withing the main program, it is working. But through the sub i m getting the reference like below. But not the content in array. [Its in sub ARRAY(0x1ea7b310)Stat2: 1340930665] – VeerM Jun 29 '12 at 00:48
  • Text::Diff documentation says: If no OUTPUT is supplied, returns the diffs in a string. Why are you assigning a string to an Array? Maybe you should start over. – Dondi Michael Stroma Jun 29 '12 at 01:10
  • i realized now you are right the function is not working when i am passing array, but works fine when using function. I am trying to rem "- " from each line, but it is not working in linux but it does work on windows.- - Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util – VeerM Jun 29 '12 at 02:24