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. - sub CleanFiles{
-
my ($file, @lines) = @_;
-
-
my @data_file = ();
-
#reset(@data_file);
-
open(DATA, $dac03_location."/".$file) or die "Cannot open the file!";
-
-
#splice(@data_file);
-
-
#Store the text in an array, easier to work with
-
while(<DATA>){
-
chomp;
-
push(@data_file,$_);
-
}
-
close(DATA);
-
-
#initialize a value to count where we are in the array in order to
-
#delete the lines we do not need
-
my $i = 0;
-
my $item;
-
my $item2;
-
foreach $item (@data_file){
-
foreach $item2 (@lines){
-
if ($item =~ /$item2/){
-
splice(@data_file,$i,1);
-
}
-
}
-
$i++
-
}
-
-
# foreach $item (@data_file){
-
# print "$item\n";
-
# }
-
-
return @data_file;
-
}
Thank you for your help :-)
6 1539
This line should solve your problems - it has solved mine whenever this problem crept up. Are you sure you are still experiencing this issue?
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
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.
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. - #!/usr/bin/perl
-
-
###################
-
#Packages required#
-
###################
-
-
use Strict;
-
use List::MoreUtils qw(uniq);
-
use Data::Dumper;
-
-
############
-
#Parameters#
-
############
-
-
#First we define the path to the file we use
-
#do not put any /
-
my $dac02_location = "/dac03_scripts";
-
my $dac03_location = "/dac03_scripts/etc";
-
-
#name of the groups
-
my @group = ('jai','smbuser','domadm');
-
#number of max user on the system
-
my $i = 8;
-
#name of additional machines that are in passwd but not in group
-
my @add_machines=('dacsrv01\$','dacwks01\$','dacwks02\$','dacwks03\$','dacwks04\$','dacwks05\$','daceufo201\$','dacluxe02\$','dacluxe03\$');
-
#special users to also update the password for
-
my @special_users=('root','nagios');
-
-
###########
-
#Functions#
-
###########
-
-
#To parse the data and store it in a hash
-
sub DataParsing{
-
my ($file, @fields) = @_;
-
-
my %data = ();
-
my $item;
-
-
open(DATA, $dac02_location."/".$file) or die "Cannot open the file!";
-
-
while(<DATA>){
-
chomp;
-
-
foreach $item (@fields){
-
if($_ =~ /$item/){
-
$data{$item}=[split('[:,]',$_)];
-
$data{$item.'_raw'} = $_;
-
}
-
}
-
}
-
-
close(DATA);
-
-
return %data;
-
}
-
-
#Clean the file group and return an array with clean data to construct the new one
-
sub CleanFiles{
-
my ($file, @lines) = @_;
-
-
my @data_file = ();
-
-
open(DATA, $dac03_location."/".$file) or die "Cannot open the file!";
-
-
#Store the text in an array, easier to work with
-
while(<DATA>){
-
chomp;
-
push(@data_file,$_);
-
}
-
close(DATA);
-
-
#initialize a value to count where we are in the array in order to
-
#delete the lines we do not need
-
my $i = 0;
-
my $item;
-
my $item2;
-
foreach $item (@data_file){
-
foreach $item2 (@lines){
-
if ($item =~ /$item2/){
-
splice(@data_file,$i,1);
-
}
-
}
-
$i++
-
}
-
-
# foreach $item (@data_file){
-
# print "$item\n";
-
# }
-
-
return @data_file;
-
}
-
-
#function to generate the new config files
-
sub CreateConfig{
-
my ($file,@data) = @_;
-
my $item;
-
-
$file = $file.".new";
-
-
open(NFILE,">$file");
-
foreach $item (@data){
-
printf NFILE "$item\n";
-
}
-
close(NFILE);
-
-
return $file;
-
}
-
-
###########
-
#Instances#
-
###########
-
-
#hash of the group with the group name as reference
-
my %group = DataParsing('group', @group);
-
#print "$group{jai}[2]\n";
-
-
#put the usernames in an array to get info out of passwd
-
my $item;
-
my @username = ();
-
foreach $item (@group){
-
for ($a = $i+1; $a > 2; $a--){
-
if($group{$item}[$a] ne ''){
-
push(@username,$group{$item}[$a])
-
}
-
}
-
}
-
-
#array containing all of the user that need to be added on system
-
my @usernames = ();
-
@usernames=uniq(@username,@add_machines);
-
-
my @special_usernames = uniq(@usernames,@special_users);
-
#hash of the user in passwd with username as reference
-
my %passwd = DataParsing('passwd',@special_usernames);
-
-
#hash of the password in shadow with username as reference
-
my %shadow = DataParsing('shadow',@special_usernames);
-
-
########################################################
-
########################################################
-
-
my @group_clean_data = CleanFiles('group',@group);
-
my @passwd_clean_data = CleanFiles('passwd',@special_usernames);
-
-
print Dumper(@passwd_clean_data);
besides the incorrect spelling of "Strict"
that should be:
I don't see anything.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |