# Disappearing hash values?

**URL:** <https://www.ruby-forum.com/t/disappearing-hash-values/61510>\
**Category:** Ruby\
**Created:** [May 24, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/disappearing-hash-values/61510 "2006-05-24T17:41:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/disappearing-hash-values/61510/1 "2006-05-24T17:41:21Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/disappearing-hash-values/61510/2 "2006-05-24T18:01:34Z")

</div>

On 5/24/06, newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/disappearing-hash-values/61510/3 "2006-05-24T18:08:00Z")

</div>

Ryan L. wrote:

> On 5/24/06, newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/disappearing-hash-values/61510/4 "2006-05-24T19:12:51Z")

</div>

On 5/24/06, newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
