473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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
6 1539
Ganon11
3,652 Expert 2GB
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
tuxy94
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
4,059 Expert 2GB
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
tuxy94
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
4,059 Expert 2GB
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
tuxy94
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

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

Similar topics

1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
2
by: Scott Navarre | last post by:
Hello, I am doing form validation using 'onChange', so that every time a text input is entered and focus is lost on that text input, it will check to make sure it is an integer: function...
4
by: Adrian Parker | last post by:
We've suddenly started getting a problem with a call to clear the contents of a DataTable. This is on a live customer site that's been working fine until yesterday. As far as we know they've not...
19
by: Krishanu Debnath | last post by:
Hello, I have a call to hash_map::clear() function which takes long time. someClass::someFunction() { // typedef hash_map<name_id, uintMp; // Mp p; // assuming proper namespace, hash...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.