Forum: Ruby disappearing hash values?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F06f8fad11810690c6e743c0fffc4017?d=identicon&s=25 newbie (Guest)
on 2006-05-24 17:41
mailbox = File.open(mbox)
    mailbox.each_line {|line|
        linecount += 1
        mboxArray.push line
        if line.include?("filename=")
            haveattach = TRUE
            filename = line.gsub("Content-Disposition: attac
hment;", '')
            filename = filename.gsub("filename=", '')
            filename = filename.gsub(/"/, '')
            filename = filename.gsub(/^ /, '')
            filename = filename.chomp + ".b64"
            filehash = {filename => linecount-2}
            print "filehash[filename] is: " + filehash[filen
ame].to_s + "\n"
            #prints 183 the first time through the file
         end

         if ( line.include?("--(null)") && haveattach )
             print "filehash[filename] is: " + filehash[filen
ame].to_s + "\n"
             #this is now nil for some reason
             print "linecount is: " + linecount.to_s + "\n"
             length = linecount-filehash[filename]
             filehash = {filename => length}
             haveattach = FALSE
         end
    }

why does the value of filehash[filename] disappear in the second "if"?
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-05-24 18:01
(Received via mailing list)
On 5/24/06, newbie <a@a.org> wrote:
>
> why does the value of filehash[filename] disappear in the second "if"?

An if statement introduces a new scope, so the filehash in the first
if is not available inside the second if. If you move the creation of
the hash outside both ifs and then just update it within the first if,
it will work as you expect.

Ryan
F06f8fad11810690c6e743c0fffc4017?d=identicon&s=25 newbie (Guest)
on 2006-05-24 18:08
Ryan Leavengood wrote:
> On 5/24/06, newbie <a@a.org> wrote:
>>
>> why does the value of filehash[filename] disappear in the second "if"?
>
> An if statement introduces a new scope, so the filehash in the first
> if is not available inside the second if. If you move the creation of
> the hash outside both ifs and then just update it within the first if,
> it will work as you expect.
>
> Ryan

i need to update it in both ifs though
i need to put a starting line and an ending line
439c401f95ee2fac0be4c1727dd74dea?d=identicon&s=25 Bira (Guest)
on 2006-05-24 19:12
(Received via mailing list)
On 5/24/06, newbie <a@a.org> wrote:
> i need to update it in both ifs though
> i need to put a starting line and an ending line

Does this work (I can't test it myself right now)?

   filehash = {}
   mailbox.each_line {|line|
       linecount += 1
       mboxArray.push line
       if line.include?("filename=")
           haveattach = TRUE
           filename = line.gsub("Content-Disposition: attachment;", '')
           filename = filename.gsub("filename=", '')
           filename = filename.gsub(/"/, '')
           filename = filename.gsub(/^ /, '')
           filename = filename.chomp + ".b64"
           filehash[filename] = linecount-2
           print "filehash[filename] is: " + filehash[filename].to_s +
"\n"
           #prints 183 the first time through the file
        end

        if ( line.include?("--(null)") && haveattach )
            print "filehash[filename] is: " + filehash[filename].to_s +
"\n"
            #this is now nil for some reason
            print "linecount is: " + linecount.to_s + "\n"
            length = linecount-filehash[filename]
            filehash[filename] = length
            haveattach = FALSE
        end
   }


Besides declaring "filehash" outside the ifs, I made a few minor
alterations, replacing the "filehash = {filename => ...}" statements
with "filehash[filename] = ...". The way it was before meant you were
throwing away the values previously stored in the hash every time that
line was executed.
This topic is locked and can not be replied to.