Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Re: Re: Re: Re: Adding and Deleting Upload File name and File

by larryk (Friar)
on Dec 05, 2001 at 19:13 UTC ( [id://129616]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Adding and Deleting Upload File name and File
in thread Adding and Deleting Upload File name and File

If you want to delete a song based on the filename, one way of doing it is to go through the songs comparing the filename to the list to delete:
# i renamed %test to %songs for sanity's sake for my $song_title (keys %songs) { my $song_file = $songs{$song_title}; if (grep $_ eq $song_file, @{$q->param('files')) { delete $song{$song_title}; unlink $song_file; } }
You say you also tried my %test = (@song_titles, @song_files); which just strings the two lists together but because a hash takes pairs of elements you found that the pairings were incorrect. This can be resolved with my initial example (see above post) of @songs{@song_titles} = @song_files;.

The thing with hashes is that the keys are all unique so to me it would make more sense if you used the filename (including path) as the key and the title (which there may be more than one instance of) as the value - this also removes your need to iterate through the songs to find the one to delete. Just an idea to make it work for you instead of the other way around.

Hope this helps,

   larryk                                          
perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
Will code for food - looking for work - London - CV

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Adding and Deleting Upload File name and File
by lex2001 (Sexton) on Dec 06, 2001 at 14:33 UTC
    Thank you for your patience and info. I got your example to work:
    @songs{@song_titles} = @song_files;
    This would place the @song_files elements as keys and the elements of @song_titles would be linked to these keys, correct? What confuses me is how it looks. It seems like it should be coded like this - since I want to make a hash
    %songs{@song_titles} = @song_files;
    moving to the deletion code that you entered above. I have been storing the song titles in a file. So if I delete one I could then print out all of the remaining values in the hash and overwrite the file correct? thanks...
      the @hashname{@keys_for_the_hash} = @values_for_those_keys notation is a hash slice which you can read more about in perldoc perldata and from which I quote:
      Entire arrays (and slices of arrays and hashes) are denoted by '@', wh +ich works much like the word "these" or "those" does in English, in t +hat it indicates multiple values are expected. @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as ($days[3],$days[4],$days[5]) @days{'a','c'} # same as ($days{'a'},$days{'c'}) Entire hashes are denoted by '%': %days # (key1, val1, key2, val2 ...)
      In answer to your second question about storing the song titles in a file - yes you can just have a subroutine that dumps all of the keys to the file overwriting the old list but is there a better way (read: more convenient for you)?

      If you do not need the file to be human-readable (i.e. you don't look through it yourself) then you might consider using a DBM - basically a perl hash operating out of a file.You can read more here.

      Hope this helps,

         larryk                                          
      perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
      Will code for food - looking for work - London - CV
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://129616]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found