[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Johnathan Smith

12/5/2007 2:42:00 PM

hi

im writing a class which so far:

- reads a text file
- and prints out the the number of Tags it encounters

however now i want 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 as the key

im unsure of how to go about this
any help or pseudo code would be greatly appreciated
my code is provided below

thanks

txt file

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

Tag: ref2
Type: Journal
Author: Smith, J

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

ruby file:

#
#
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
--
Posted via http://www.ruby-....

13 Answers

Robert Klemme

12/5/2007 3:20:00 PM

0

2007/12/5, Johnathan Smith <stu_09@hotmail.com>:
> hi
>
> im writing a class which so far:
>
> - reads a text file
> - and prints out the the number of Tags it encounters
>
> however now i want 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 as the key
>
> im unsure of how to go about this
> any help or pseudo code would be greatly appreciated
> my code is provided below
>
> thanks
>
> .txt file
>
> Tag: ref1
> Type: Book
> Author: Little, S R
>
> Tag: ref2
> Type: Journal
> Author: Smith, J
>
> Tag: ref3
> Type: Conference Paper
> Author: Williams, M
>
> ruby file:
>
> #
> #
> 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
> --
> Posted via http://www.ruby-....

Why do you repost the same question when you got answers already? Are
those answers not propagated to ruby-forum?

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Johnathan Smith

12/5/2007 3:26:00 PM

0

> Why do you repost the same question when you got answers already? Are
> those answers not propagated to ruby-forum?

hi

i wasnt sure i had recieved the answers i was looking for. I was
provided with a few different bits of code which im very grateful for
but I was unable to get working. one of the solutions provided by the
previous threat i will post below. Im not provided with any output at
all. I was to maintain the tag count as well as creating the hash. if
you could point out my errors id be extremly grateful

thanks

#
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

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
#
#
#

--
Posted via http://www.ruby-....

MonkeeSage

12/5/2007 4:05:00 PM

0

On Dec 5, 9:26 am, Johnathan Smith <stu...@hotmail.com> wrote:
> > Why do you repost the same question when you got answers already? Are
> > those answers not propagated to ruby-forum?
>
> hi
>
> i wasnt sure i had recieved the answers i was looking for. I was
> provided with a few different bits of code which im very grateful for
> but I was unable to get working. one of the solutions provided by the
> previous threat i will post below. Im not provided with any output at
> all. I was to maintain the tag count as well as creating the hash. if
> you could point out my errors id be extremly grateful
>
> thanks
>
> #
> 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
>
> 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
> #
> #
> #
>
> --
> Posted viahttp://www.ruby-....

If you don't understand or don't find an answer sufficient, say so in
the same thread, no one will be mad. But if you start a new one, then
people following the old one aren't helped if you get the answer in
the new one.

Assuming that your text file is always in the format you listed, this
may be what you're after...

database = {}
File.open('reference.txt') { | handle |
tags = handle.read.split("\n\n")
for tag in tags
key, type, author = tag.split("\n")
database[key[5..-1]] = [type[6..-1], author[9..-1]]
end
}
p database

Given your sample data, this will output the following...

{"ref1"=>["Book", "Little, S R"], "ref2"=>["Journal", "Smith, J"],
"ref3"=>["Conference Paper", "Williams, M"]}

And the number of tags seen is just "database.length".

Regards,
Jordan

Johnathan Smith

12/5/2007 8:28:00 PM

0

hi,

thanks, i've got it to print out the hash
although I'm getting a wierd output where its taking the first letter
off the surname e.g:

>ruby main.rb
3
{"ref1"=>["Book", "ittle, S R"], "ref2"=>["Journal", "mith, J"]

can you see in my code where im going wrong?
also, is it possible to print the different refrences on new lines?

ill provide my code below
thanks

#
#
File.open('reference.txt').each do |line|
if line =~ /^tag:/i
$linecount += 1
end
end
puts $linecount
#
#
#
File.open('reference.txt') { | handle |tags =
handle.read.split("\n\n")
for tag in tags
key, type, author = tag.split("\n")
database[key[5..-1]] = [type[6..-1], author[9..-1]]
end
}
p database
#
#
--
Posted via http://www.ruby-....

MonkeeSage

12/5/2007 8:50:00 PM

0

On Dec 5, 2:28 pm, Johnathan Smith <stu...@hotmail.com> wrote:
> hi,
>
> thanks, i've got it to print out the hash
> although I'm getting a wierd output where its taking the first letter
> off the surname e.g:
>
> >ruby main.rb
>
> 3
> {"ref1"=>["Book", "ittle, S R"], "ref2"=>["Journal", "mith, J"]
>
> can you see in my code where im going wrong?
> also, is it possible to print the different refrences on new lines?
>
> ill provide my code below
> thanks
>
> #
> #
> File.open('reference.txt').each do |line|
> if line =~ /^tag:/i
> $linecount += 1
> end
> end
> puts $linecount
> #
> #
> #

^ This part is pointless now. $linecount above is equal to
database.length in the code below.

> File.open('reference.txt') { | handle |tags =
> handle.read.split("\n\n")
> for tag in tags
> key, type, author = tag.split("\n")
> database[key[5..-1]] = [type[6..-1], author[9..-1]]
> end
> }
> p database
> #
> #
> --
> Posted viahttp://www.ruby-....

It should have been author[8..-1], sorry about that.

I suggest that you go through one of the many ruby guides/tutorials
listed here: http://www.ruby-lang.org/en/docu...

Regards,
Jordan

Johnathan Smith

12/5/2007 9:01:00 PM

0

cheers.

yer i have been reading many tutorials but was unable to grasp what I
was trying to achieve.

one for thing i was wondering:

if i was to extend the some of the information in the .txt file
e.g:

Tag: ref1
Type: Book
Author: Smith, J
Publisher: New Books
Chapter: 12

could i adapt the code so "any other information" is put into the hash
rather than having set information like author or type?

thanks

--
Posted via http://www.ruby-....

Phrogz

12/5/2007 10:02:00 PM

0

On Dec 5, 2:00 pm, Johnathan Smith <stu...@hotmail.com> wrote:
> yer i have been reading many tutorials but was unable to grasp what I
> was trying to achieve.
>
> one for thing i was wondering:
>
> if i was to extend the some of the information in the .txt file
> e.g:
>
> Tag: ref1
> Type: Book
> Author: Smith, J
> Publisher: New Books
> Chapter: 12
>
> could i adapt the code so "any other information" is put into the hash
> rather than having set information like author or type?

Yes, both of the pieces of code I wrote for you originally handle this
case just fine. Please read through the code and understand it. You
asked for pseudo-code originally, and I actually gave you fully-
functioning code. Now you just need to understand how to use it and
its output.

MonkeeSage

12/5/2007 10:20:00 PM

0

On Dec 5, 4:02 pm, Phrogz <phr...@mac.com> wrote:
> On Dec 5, 2:00 pm, Johnathan Smith <stu...@hotmail.com> wrote:
>
>
>
> > yer i have been reading many tutorials but was unable to grasp what I
> > was trying to achieve.
>
> > one for thing i was wondering:
>
> > if i was to extend the some of the information in the .txt file
> > e.g:
>
> > Tag: ref1
> > Type: Book
> > Author: Smith, J
> > Publisher: New Books
> > Chapter: 12
>
> > could i adapt the code so "any other information" is put into the hash
> > rather than having set information like author or type?
>
> Yes, both of the pieces of code I wrote for you originally handle this
> case just fine. Please read through the code and understand it. You
> asked for pseudo-code originally, and I actually gave you fully-
> functioning code. Now you just need to understand how to use it and
> its output.

Sorry about that Phrogz. I didn't read the other thread, I just
glanced at it briefly the other day. I didn't realize you had already
answered this fully.

Regards,
Jordan

Phrogz

12/5/2007 10:30:00 PM

0

On Dec 5, 3:19 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 5, 4:02 pm, Phrogz <phr...@mac.com> wrote:
> > Yes, both of the pieces of code I wrote for you originally handle this
> > case just fine. Please read through the code and understand it. You
> > asked for pseudo-code originally, and I actually gave you fully-
> > functioning code. Now you just need to understand how to use it and
> > its output.
>
> Sorry about that Phrogz. I didn't read the other thread, I just
> glanced at it briefly the other day. I didn't realize you had already
> answered this fully.

Absolutely no need for an apology. Helping people isn't a competition
of who can answer first. And, if I choose to stop helping someone
because I've done as much as I'm willing to do, there's certainly no
etiquette that says you shouldn't step in.

I'm glad you helped Johnathan get further towards his solution that I
did, and certainly I don't care that you used your own code versus
mine.

But I appreciate the sentiment. :)

Johnathan Smith

12/5/2007 10:50:00 PM

0

hey guys

sorry for the whole mix up! I appreciate all the help I've received
especially as im new to ruby! im also new to the forum and should have
tried to stick it our with the first threat! so i hope i havn't offended
any of you by this.

that aside, i really hope you can help me resolve this problem.

Jordan, the code you gave me worked successfully. However, it just deals
with the the two types i gave you, author and type.Its partially my
fault for not explaining properly becuase i hope it could deal with any
information after the tag. is this possible to achieve?

again sorry for the mix up
cheers



--
Posted via http://www.ruby-....