From: Robert Klemme Date: 2008-08-20T01:26:40+09:00 Subject: Re: Find element in array of hashes On 19.08.2008 18:13, Adgar Marks wrote: > Dear all, > > i am trying to find my way in Ruby, but i am just a bignner. Here is my > problem: > > I have two array of hashes: > > _DownloadData << { :FileName => splitLine[0] , > :Size => splitLine[4].to_i } > > _FilesData << { :FileName => splitLine[8] , > :Year => year , > :Month => month , > :Day => day , > :Suffix => suffix , > :JDay => jday , > :Size => splitLine[4].to_i } I assume _DownloadData and _FilesData are of type Array. > I would like to know if FileName from _FileesData is in _DownloadData > and later or if the two files have the same size. > > I tried to write it like this: > if _DownloadData[:FileName].include?( _FilesData[:FileName] ) > > and i already got an error. Well, if they are arrays you rather need something like this: _DownloadData.each |dl| x = _FilesData.find {|fl| fl[:FileName] == dl[:FileName]} if x # found end end But note that this is inefficient if you have multiple entries in both arrays. In that case building a hash with the file name as key could speed up things considerably. Btw, conventionally variables in Ruby have all lowercase names with underscores and no leading underscore. Kind regards robert