Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Remove From List

by PyrexKidd (Monk)
on Sep 09, 2010 at 19:46 UTC ( #859551=perlquestion: print w/ replies, xml ) Need Help??
PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a program that removes an entry from one of three lists. The program appears to work except the new files conf_list_* are empty except for the entries deleted. What am I missing?

#!/usr/bin/perl use strict; use warnings; use File::Copy ("cp"); my $announce_off = "master_conf_list_announce_off"; my $announce_on = "master_conf_list_announce_on"; my $beep_on = "master_conf_list_beep_on"; my $date = &getDate; my (%announce_off, %announce_on, %beep_on); print "$date\n"; #create Backup files in the backup folder. just incase anything goes +wrong. cp ("./master_list/$announce_off", "./master_list/backup/$announce_off +" . $date); cp ("./master_list/$announce_on", "./master_list/backup/$announce_on" +. $date); cp ("./master_list/$beep_on", "./master_list/backup/$beep_on" . $date) +; #create a scratch pad cp ("./master_list/$announce_off", "./master_list/$announce_off\.bak") +; cp ("./master_list/$announce_on", "./master_list/$announce_on\.bak"); cp ("./master_list/$beep_on", "./master_list/$beep_on\.bak"); #delete original files unlink ("./master_list/$announce_off"); unlink ("./master_list/$announce_on"); unlink ("./master_list/$beep_on"); #open files for reading open my $ANNOUNCE_OFFFH, '<', "./master_list/$announce_off\.bak"; open my $ANNOUNCE_ONFH, '<', "./master_list/$announce_on\.bak"; open my $BEEP_ONFH, '<', "./master_list/$beep_on\.bak"; open my $DELETE_LISTFH, '<', "$ARGV[0]"; #open files for writing open my $ANNOUNCE_OFFNEW, '>>', "./master_list/$announce_off"; open my $ANNOUNCE_ONNEW, '>>', "./master_list/$announce_on"; open my $BEEP_ONNEW, '>>', "./master_list/$beep_on"; open my $DELETED_ITEMS, '>>', "./deleted_items.txt"; #slurp each file into hash foreach (<$ANNOUNCE_OFFFH>){ chomp($_); $announce_off{$_} = 1; } foreach(<$ANNOUNCE_ONFH>){ chomp($_); $announce_on{$_} =1; } foreach(<$BEEP_ONFH>){ chomp($_); $beep_on{$_} = 1; } #parse through the list to delete for items to delete #print in either the old location or a liste of deleted files foreach (<$DELETE_LISTFH>){ chomp($_); print { exists $announce_off{$_} ? $ANNOUNCE_OFFNEW : $DELETED_ITE +MS } $_ ."\n"; print { exists $announce_on{$_} ? $ANNOUNCE_ONNEW : $DELETED_ITEMS +} $_ ."\n"; print { exists $beep_on{$_} ? $BEEP_ONNEW : $DELETED_ITEMS } $_ . +"\n"; } close $ANNOUNCE_OFFNEW; close $ANNOUNCE_ONNEW; close $BEEP_ONNEW; close $DELETED_ITEMS; sub getDate(){ my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my(@weekDays) = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $day +OfWeek, $dayOfYear, $daylightSavings) = localtime(); my($year) = 1900 + $yearOffset; my($theTime) = "\_$dayOfMonth\_$months[$month]\_$hour$minute"; return $theTime; }

Thanks for any assistance in advance.

EDIT:

ok, so I know I have it backwards. When I parse through my files I don't actually DELETE the item, I just choose which file to print it to. Since I am just testing if a key exists, and if it does, I am printing it to a file. So the reason I come up with empty files is that I never print anything to my files...

rookie mistake

So this leads me to my next question, how do I remove said entries from the hash? I'll check Google. Does any one have any suggestions on a better way to do this? I'd like any comments/constructive criticisms.

Comment on Remove From List
Download Code
Replies are listed 'Best First'.
Re: Remove From List
by Krambambuli (Deacon) on Sep 09, 2010 at 20:57 UTC
    Sorry... forget my (first) answer, I totally misread your question.

    Use Perl's "delete" function to delete from a hash.


    Krambambuli
    ---

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://859551]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2015-07-31 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls