By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

How to clear the data each time I call a function?

P: 4
I do not understand why the variable @data_file keeps the data from the previous call.
My goal is to use this in order to clean a file from specific lines I do not desire, I do so, but if I run this method twice, it will append the data
of the current call after the data from the previous call. Is there a way I can reset the @data_file each time the function is called or is there something I can do to help?

Here is the code for the function: (do not be too hard I have only been coding in Perl for the past 2 days!) Also I use Strict at the beginning of the program.



Expand|Select|Wrap|Line Numbers
  1. sub CleanFiles{
  2.     my ($file, @lines) = @_;
  3.  
  4.     my @data_file = ();
  5. #reset(@data_file);
  6.     open(DATA, $dac03_location."/".$file) or die "Cannot open the file!";
  7.  
  8. #splice(@data_file);
  9.  
  10.     #Store the text in an array, easier to work with
  11.     while(<DATA>){
  12.         chomp;
  13.         push(@data_file,$_);
  14.     }
  15.     close(DATA);
  16.  
  17.     #initialize a value to count where we are in the array in order to 
  18.     #delete the lines we do not need
  19.     my $i = 0;
  20.     my $item;
  21.     my $item2;
  22.     foreach $item (@data_file){
  23.         foreach $item2 (@lines){
  24.             if ($item =~ /$item2/){
  25.                 splice(@data_file,$i,1);
  26.             }
  27.         }
  28.         $i++
  29.     }
  30.  
  31. #    foreach $item (@data_file){
  32. #        print "$item\n";
  33. #    }
  34.  
  35.     return @data_file;
  36. }
Thank you for your help :-)
Apr 1 '08 #1
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. my @data_file = ();
This line should solve your problems - it has solved mine whenever this problem crept up. Are you sure you are still experiencing this issue?
Apr 1 '08 #2

P: 4
Expand|Select|Wrap|Line Numbers
  1. my @data_file = ();
This line should solve your problems - it has solved mine whenever this problem crept up. Are you sure you are still experiencing this issue?
Yes I actually use that line @data_file = (); and I still have the problem, I am not sure why... It does work but the data in the array @data_file is not reset to 0 each time I call the function, so I end up with a lot of data I do not want.

Do you have any other suggestions?

Thank you
Apr 1 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Once you do this:

@array = ();

any values the array had before will be gone. So the problem is being caused elsewhere in the program or you are confused about something.
Apr 1 '08 #4

P: 4
Well here is the entire script that I am writing, but alas I am still having the problem and cannot figure out where it may be. Maybe you will know better.

Thanks a lot.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. ###################
  4. #Packages required#
  5. ###################
  6.  
  7. use Strict;
  8. use List::MoreUtils qw(uniq);
  9. use Data::Dumper;
  10.  
  11. ############
  12. #Parameters#
  13. ############
  14.  
  15. #First we define the path to the file we use
  16. #do not put any /
  17. my $dac02_location = "/dac03_scripts";
  18. my $dac03_location = "/dac03_scripts/etc";
  19.  
  20. #name of the groups
  21. my @group = ('jai','smbuser','domadm');
  22. #number of max user on the system
  23. my $i = 8;
  24. #name of additional machines that are in passwd but not in group
  25. my @add_machines=('dacsrv01\$','dacwks01\$','dacwks02\$','dacwks03\$','dacwks04\$','dacwks05\$','daceufo201\$','dacluxe02\$','dacluxe03\$');
  26. #special users to also update the password for
  27. my @special_users=('root','nagios');
  28.  
  29. ###########
  30. #Functions#
  31. ###########
  32.  
  33. #To parse the data and store it in a hash
  34. sub DataParsing{
  35.     my ($file, @fields) = @_;
  36.  
  37.     my %data = ();
  38.     my $item;
  39.  
  40.     open(DATA, $dac02_location."/".$file) or die "Cannot open the file!";
  41.  
  42.     while(<DATA>){
  43.         chomp;
  44.  
  45.         foreach $item (@fields){
  46.             if($_ =~ /$item/){
  47.                 $data{$item}=[split('[:,]',$_)];
  48.                 $data{$item.'_raw'} = $_;
  49.             }
  50.         }
  51.     }
  52.  
  53.     close(DATA);
  54.  
  55.     return %data;
  56. }
  57.  
  58. #Clean the file group and return an array with clean data to construct the new one
  59. sub CleanFiles{
  60.     my ($file, @lines) = @_;
  61.  
  62.     my @data_file = ();
  63.  
  64.     open(DATA, $dac03_location."/".$file) or die "Cannot open the file!";
  65.  
  66.     #Store the text in an array, easier to work with
  67.     while(<DATA>){
  68.         chomp;
  69.         push(@data_file,$_);
  70.     }
  71.     close(DATA);
  72.  
  73.     #initialize a value to count where we are in the array in order to
  74.     #delete the lines we do not need
  75.     my $i = 0;
  76.     my $item;
  77.     my $item2;
  78.     foreach $item (@data_file){
  79.         foreach $item2 (@lines){
  80.             if ($item =~ /$item2/){
  81.                 splice(@data_file,$i,1);
  82.             }
  83.         }
  84.         $i++
  85.     }
  86.  
  87. #    foreach $item (@data_file){
  88. #        print "$item\n";
  89. #    }
  90.  
  91.     return @data_file;
  92. }
  93.  
  94. #function to generate the new config files
  95. sub CreateConfig{
  96.     my ($file,@data) = @_;
  97.     my $item;
  98.  
  99.     $file = $file.".new";
  100.  
  101.     open(NFILE,">$file");
  102.     foreach $item (@data){
  103.         printf NFILE "$item\n";
  104.     }
  105.     close(NFILE);
  106.  
  107.     return $file;
  108. }
  109.  
  110. ###########
  111. #Instances#
  112. ###########
  113.  
  114. #hash of the group with the group name as reference
  115. my %group = DataParsing('group', @group);
  116. #print "$group{jai}[2]\n";
  117.  
  118. #put the usernames in an array to get info out of passwd
  119. my $item;
  120. my @username = ();
  121. foreach $item (@group){
  122.     for ($a = $i+1; $a > 2; $a--){
  123.         if($group{$item}[$a] ne ''){
  124.             push(@username,$group{$item}[$a])
  125.         }
  126.     }
  127. }
  128.  
  129. #array containing all of the user that need to be added on system
  130. my @usernames = ();
  131. @usernames=uniq(@username,@add_machines);
  132.  
  133. my @special_usernames = uniq(@usernames,@special_users);
  134. #hash of the user in passwd with username as reference
  135. my %passwd = DataParsing('passwd',@special_usernames);
  136.  
  137. #hash of the password in shadow with username as reference
  138. my %shadow = DataParsing('shadow',@special_usernames);
  139.  
  140. ########################################################
  141. ########################################################
  142.  
  143. my @group_clean_data = CleanFiles('group',@group);
  144. my @passwd_clean_data = CleanFiles('passwd',@special_usernames);
  145.  
  146. print Dumper(@passwd_clean_data);
Apr 2 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
besides the incorrect spelling of "Strict"

Expand|Select|Wrap|Line Numbers
  1. use Strict;
that should be:

Expand|Select|Wrap|Line Numbers
  1. use strict;
I don't see anything.
Apr 2 '08 #6

P: 4
I did all of the modification you guys suggested and still, I run into the same problem. I have also tried to change DATA to something else but still I do not know what to do.

Any other ideas?

Thanks again.
Apr 2 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.