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: (do not be too hard I have only been coding in Perl for the past 2 days!) 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);


In reply to Why does the array not reset each time I call the function? by tuxy94

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.