hash

  • Thread starter Johnathan Smith
  • Start date
J

Johnathan Smith

Hi

iv started writing a class which reads the data from a text file and
counts the number of Tags

however im having a struggle as I want to split the tag and the data
into a hash i.e the Tag is the index and the data is the rest

if anyone could provide me with any psuedo code i'd be very appreciative
my code is below

Many Thanks

text file:

Tag: ref1
Type: Book
Author: Little, S R

Tag: ref2
Type: Journal
Author: Smith, J

Tag: ref3
Type: Conference Paper
Author: Williams, M

Tag: ref4
Type: Book
Author: Jones, M B


ruby class:

#!/usr/local/bin/ruby
#
#
#
require 'getoptlong'

opts = GetoptLong.new(
['--style', '-n', GetoptLong::NO_ARGUMENT ],
['--database', '-i', GetoptLong::REQUIRED_ARGUMENT]
)

$linecount = 0

opts.each do |opt, arg|
case opt
when '--style'
require arg
when '--database'
end
end
#
#
#
# process options
#
#
#
File.open('reference.txt').each do |line|
if line =~ /^tag:/i
$linecount += 1
end
end
puts $linecount
#
 
I

Ilan Berci

Johnathan said:
File.open('reference.txt').each do |line|
if line =~ /^tag:/i
$linecount += 1
end
end
puts $linecount
#

try something like this:

linecount = 0
results = []
hash = {}
File.open('reference.txt').each do |line|
m = line.match /^(\w+):\s*([\w+,\s]+)$/
unless m
results << hash unless hash.empty?
hash = {}
else
linecount += 1
hash[m[1]] = m[2].chomp
end
end
 
P

Phrogz

iv started writing a class which reads the data from a text file and
counts the number of Tags

however im having a struggle as I want to split the tag and the data
into a hash i.e the Tag is the index and the data is the rest

if anyone could provide me with any psuedo code i'd be very appreciative
my code is below

Many Thanks

text file:

Tag: ref1
Type: Book
Author: Little, S R

Tag: ref2
Type: Journal
Author: Smith, J

Tag: ref3
Type: Conference Paper
Author: Williams, M

Tag: ref4
Type: Book
Author: Jones, M B

info = {}
last_tag = nil

# Could be File.readlines
DATA.each_line{ |line|
_, key, data = line.match( /^(\w+): (.+)/ ).to_a
next unless key # skip blank lines
if key == "Tag"
last_tag = data
info[ data ] = {}
else
info[ last_tag ][ key ] = data
end
}

require 'pp'
pp info
#=> {"ref4 "=>{"Author"=>"Jones, M B", "Type"=>"Book "},
#=> "ref3 "=>{"Author"=>"Williams, M ", "Type"=>"Conference Paper "},
#=> "ref2 "=>{"Author"=>"Smith, J ", "Type"=>"Journal "},
#=> "ref1 "=>{"Author"=>"Little, S R ", "Type"=>"Book "}}

__END__
Tag: ref1
Type: Book
Author: Little, S R

Tag: ref2
Type: Journal
Author: Smith, J

Tag: ref3
Type: Conference Paper
Author: Williams, M

Tag: ref4
Type: Book
Author: Jones, M B
 
P

Phrogz

Here's another variation:

# Could be IO.read
info = Hash[ *DATA.read.split( /\n\n+/ ).map{ |chunk|
pieces = chunk.scan /^(\w+): (.+)/
first = pieces.shift
raise "Uhm...I was assuming Tag comes first" unless first[0] ==
"Tag"
[ first[1], Hash[ *pieces.flatten ] ]
}.flatten ]

require 'pp'
pp info
#=> {"ref4 "=>{"Author"=>"Jones, M B", "Type"=>"Book "},
#=> "ref3 "=>{"Author"=>"Williams, M ", "Type"=>"Conference Paper "},
#=> "ref2 "=>{"Author"=>"Smith, J ", "Type"=>"Journal "},
#=> "ref1 "=>{"Author"=>"Little, S R ", "Type"=>"Book "}}

__END__
Tag: ref1
Type: Book
Author: Little, S R

Tag: ref2
Type: Journal
Author: Smith, J

Tag: ref3
Type: Conference Paper
Author: Williams, M

Tag: ref4
Type: Book
Author: Jones, M B
 
J

Johnathan Smith

Hi,

thanks for the awesome response I appreciate it

however, i think i may have misunderstood what i was trying to achieve.

what i want to do is to create an empty hash every time it encounters a
Tag
line, and if it encounters any other field, put the field and the
related
value in the hash, using the field name (e.g Type and Author) as the key

im not sure if any of the help you provived achieves this but any help
on this matter would be greatly appreciated

many thanks
 
P

Phrogz

what i want to do is to create an empty hash every time it encounters a
Tag
line, and if it encounters any other field, put the field and the
related
value in the hash, using the field name (e.g Type and Author) as the key

im not sure if any of the help you provived achieves this but any help
on this matter would be greatly appreciated

Unless I'm misunderstanding you, both of the solutions I provided do
what you are describing.

You can even see this logic explicitly here, in the first solution:
if key == "Tag"
last_tag = data
info[ data ] = {}
else
info[ last_tag ][ key ] = data
end


Does the output not look like what you expect?

pp info
#=> {"ref4 "=>{"Author"=>"Jones, M B", "Type"=>"Book "},
#=> "ref3 "=>{"Author"=>"Williams, M ", "Type"=>"Conference Paper
"},
#=> "ref2 "=>{"Author"=>"Smith, J ", "Type"=>"Journal "},
#=> "ref1 "=>{"Author"=>"Little, S R ", "Type"=>"Book "}}

If not, what actual output did you want?
 
J

Johnathan Smith

im just expecting an output of every time it encounters a tag to create
an empty hash containing the other fields using the fields as the key

im having trouble getting the code u provided to work can you see my
errors?

#
#
require 'getoptlong'

opts = GetoptLong.new(
['--style', '-n', GetoptLong::NO_ARGUMENT ],
['--database', '-i', GetoptLong::REQUIRED_ARGUMENT]
)

$linecount = 0

opts.each do |opt, arg|
case opt
when '--style'
require arg
when '--database'
end
end
#
#
#
# process options
#
#
#
File.open('reference.txt').each do |line|
if line =~ /^tag:/i
$linecount += 1
end
end
puts $linecount

info = {}
last_tag = nil

data.each_line{ |line| _, key, data = line.match( /^(\w+): (.+)/
).to_a
next unless key # skip blank lines
if key == "Tag"
last_tag = data
info[ data ] = {}
else
info[ last_tag ][ key ] = data
end
}
#
#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top