[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Comparing two arrays

Rafael George

2/27/2007 5:42:00 PM

Hi, im trying to compare two arrays and creating a new one with the
values that are in the second array and the values that aren't
concatenated with "NOT FOUND", here is the snippet code. Im getting a
weird result im getting the same values saying "not found" and without
the concatanation, can somebody please give me a hint of what is going
on.

Thanks in advance.

def handle_data(data1,data2)
c = 0
t = 0
result = []
while c < data1.length
k = 0
while k < data2.length
if data1[c].length > 7
result[t] = data1[c].include?(data2[k]) ? data1[c] :
data1[c]+" NOT FOUND"
t += 1
end
k += 1
end
c += 1
end
return result.uniq
end


--
Grimoire Guru
SourceMage GNU/Linux

14 Answers

Robert Dober

2/27/2007 5:58:00 PM

0

On 2/27/07, Rafael George <george.rafael@gmail.com> wrote:
> Hi, im trying to compare two arrays and creating a new one with the
> values that are in the second array and the values that aren't
> concatenated with "NOT FOUND", here is the snippet code. Im getting a
> weird result im getting the same values saying "not found" and without
> the concatanation, can somebody please give me a hint of what is going
> on.
>
> Thanks in advance.
>
> def handle_data(data1,data2)
> c = 0
> t = 0
> result = []
> while c < data1.length
> k = 0
> while k < data2.length
> if data1[c].length > 7
> result[t] = data1[c].include?(data2[k]) ? data1[c] :
> data1[c]+" NOT FOUND"
> t += 1
> end
> k += 1
> end
> c += 1
> end
> return result.uniq
> end
>
>
> --
> Grimoire Guru
> SourceMage GNU/Linux
>
>
If I understand correctly you want something like

ary1.map{ |ele| ele.to_s << ( ary2.include?( ele ) ? "" : " NOT FOUND" ) }

HTH
Robert

BTW did I read an article of you in the French Linux Mag recently?

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

Rafael George

2/27/2007 6:05:00 PM

0

Ok, from what i understand you are iterating thought the entire first
array and then testing the content of it in the second, thats seems
ok. Let me check if that can solve my problem.

And, related to the article i don't know for sure, i wrote something
in the past but it was in spanish can you point me to the article or
at least the title of it.

Thanks.
On 2/27/07, Robert Dober <robert.dober@gmail.com> wrote:
> On 2/27/07, Rafael George <george.rafael@gmail.com> wrote:
> > Hi, im trying to compare two arrays and creating a new one with the
> > values that are in the second array and the values that aren't
> > concatenated with "NOT FOUND", here is the snippet code. Im getting a
> > weird result im getting the same values saying "not found" and without
> > the concatanation, can somebody please give me a hint of what is going
> > on.
> >
> > Thanks in advance.
> >
> > def handle_data(data1,data2)
> > c = 0
> > t = 0
> > result = []
> > while c < data1.length
> > k = 0
> > while k < data2.length
> > if data1[c].length > 7
> > result[t] = data1[c].include?(data2[k]) ? data1[c] :
> > data1[c]+" NOT FOUND"
> > t += 1
> > end
> > k += 1
> > end
> > c += 1
> > end
> > return result.uniq
> > end
> >
> >
> > --
> > Grimoire Guru
> > SourceMage GNU/Linux
> >
> >
> If I understand correctly you want something like
>
> ary1.map{ |ele| ele.to_s << ( ary2.include?( ele ) ? "" : " NOT FOUND" ) }
>
> HTH
> Robert
>
> BTW did I read an article of you in the French Linux Mag recently?
>
> --
> We have not succeeded in answering all of our questions.
> In fact, in some ways, we are more confused than ever.
> But we feel we are confused on a higher level and about more important things.
> -Anonymous
>
>


--
Grimoire Guru
SourceMage GNU/Linux

Gavin Kistner

2/27/2007 6:12:00 PM

0

On Feb 27, 10:41 am, "Rafael George" <george.raf...@gmail.com> wrote:
> Hi, im trying to compare two arrays and creating a new one with the
> values that are in the second array and the values that aren't
> concatenated with "NOT FOUND", here is the snippet code.

Ruby makes it quite easy to find out which elements from one array
aren't in the other:

irb(main):001:0> a = [ 'a', 'b', 'c', 'd' ]
=> ["a", "b", "c", "d"]
irb(main):002:0> b = [ 'c', 'b', 'd', 'e' ]
=> ["c", "b", "d", "e"]
irb(main):003:0> a - b # Items from a not in b.
=> ["a"]
irb(main):004:0> b - a # Items from b not in a.
=> ["e"]
irb(main):006:0> a | b # Items shared between both.
=> ["a", "b", "c", "d", "e"]

Rafael George

2/27/2007 6:43:00 PM

0

Ok, thanks now im trying to take out this from a anyline on the file

-----
or
[a-zA-Z] or #

this is the code that im using, but the lines stays with something
because my script parse that line. Another suggestion ?
delete("#[a-zA-Z]-").strip


On 2/27/07, Phrogz <gavin@refinery.com> wrote:
> On Feb 27, 10:41 am, "Rafael George" <george.raf...@gmail.com> wrote:
> > Hi, im trying to compare two arrays and creating a new one with the
> > values that are in the second array and the values that aren't
> > concatenated with "NOT FOUND", here is the snippet code.
>
> Ruby makes it quite easy to find out which elements from one array
> aren't in the other:
>
> irb(main):001:0> a = [ 'a', 'b', 'c', 'd' ]
> => ["a", "b", "c", "d"]
> irb(main):002:0> b = [ 'c', 'b', 'd', 'e' ]
> => ["c", "b", "d", "e"]
> irb(main):003:0> a - b # Items from a not in b.
> => ["a"]
> irb(main):004:0> b - a # Items from b not in a.
> => ["e"]
> irb(main):006:0> a | b # Items shared between both.
> => ["a", "b", "c", "d", "e"]
>
>
>


--
Grimoire Guru
SourceMage GNU/Linux

Gavin Kistner

2/27/2007 6:52:00 PM

0

On Feb 27, 11:42 am, "Rafael George" <george.raf...@gmail.com> wrote:
> Ok, thanks now im trying to take out this from a anyline on the file
>
> -----
> or
> [a-zA-Z] or #
>
> this is the code that im using, but the lines stays with something
> because my script parse that line. Another suggestion ?
> delete("#[a-zA-Z]-").strip

I'm sorry, but I do not understand at all what you are asking. I
assume English isn't your first language (and that's OK). Could you
try writing it a different way, so maybe I can help you?

P.S. It's considered bad form on the Internet and particularly this
forum to "top post", putting your reply above the original text. In
the future, please try to put your reply after the quoted text, so
that the original flow of responses continues from top to bottom.

Rafael George

2/27/2007 7:07:00 PM

0

On 2/27/07, Phrogz <gavin@refinery.com> wrote:
> On Feb 27, 11:42 am, "Rafael George" <george.raf...@gmail.com> wrote:
> > Ok, thanks now im trying to take out this from a anyline on the file
> >
> > -----
> > or
> > [a-zA-Z] or #
> >
> > this is the code that im using, but the lines stays with something
> > because my script parse that line. Another suggestion ?
> > delete("#[a-zA-Z]-").strip
>
> I'm sorry, but I do not understand at all what you are asking. I
> assume English isn't your first language (and that's OK). Could you
> try writing it a different way, so maybe I can help you?
>
> P.S. It's considered bad form on the Internet and particularly this
> forum to "top post", putting your reply above the original text. In
> the future, please try to put your reply after the quoted text, so
> that the original flow of responses continues from top to bottom.
>
>
>

Sorry for that about the post i did not know. Maybe if i show you some
code you can check what am i trying to do.

Anyway i will try to make a simple explanation:
I parsing a CSV file using the module from the Ruby standard lib, by
far i have the parsing ok and the comparation too. So the problem is
that i don't need to fieldnames to get in the comparation process, so
i was trying to take out the letters and - and # from the output. The
problem now is that i just notice that some columns in the data have
letters too so if i take out that it won't make any sense.

If you have a suggestion can be a of a lot of help, thanks


data = []
i = 0
IO.foreach(filename) do |line|
cols = []
cols=CSV::parse_line line.delete("#[a-zA-Z]-").strip
if (cols[args[0]] != nil) && (cols[args[1]] != nil)
if cols[args[0]].slice(0,1).to_i > 0 && cols[args[0]].length < 7-
cols[args[0]].chop.strip
cols[args[1]].chop.strip
data[i] = "0"+cols[args[0]]+"-"+cols[args[1]]
else
data[i] = cols[args[0]].sub(/[^\w]/,'')+"-"+cols[args[1]]
end
i += 1
end
79 end
80 return data


--
Grimoire Guru
SourceMage GNU/Linux

Robert Dober

2/27/2007 8:26:00 PM

0

On 2/27/07, Rafael George <george.rafael@gmail.com> wrote:
> On 2/27/07, Phrogz <gavin@refinery.com> wrote:
> > On Feb 27, 11:42 am, "Rafael George" <george.raf...@gmail.com> wrote:
> > > Ok, thanks now im trying to take out this from a anyline on the file
> > >
> > > -----
> > > or
> > > [a-zA-Z] or #
> > >
> > > this is the code that im using, but the lines stays with something
> > > because my script parse that line. Another suggestion ?
> > > delete("#[a-zA-Z]-").strip
> >
> > I'm sorry, but I do not understand at all what you are asking. I
> > assume English isn't your first language (and that's OK). Could you
> > try writing it a different way, so maybe I can help you?
> >
> > P.S. It's considered bad form on the Internet and particularly this
> > forum to "top post", putting your reply above the original text. In
> > the future, please try to put your reply after the quoted text, so
> > that the original flow of responses continues from top to bottom.
> >
> >
> >
>
> Sorry for that about the post i did not know. Maybe if i show you some
> code you can check what am i trying to do.
>
> Anyway i will try to make a simple explanation:
> I parsing a CSV file using the module from the Ruby standard lib, by
> far i have the parsing ok and the comparation too. So the problem is
> that i don't need to fieldnames to get in the comparation process, so
> i was trying to take out the letters and - and # from the output. The
> problem now is that i just notice that some columns in the data have
> letters too so if i take out that it won't make any sense.
>
> If you have a suggestion can be a of a lot of help, thanks
>
>
> data = []
> i = 0
> IO.foreach(filename) do |line|
> cols = []
> cols=CSV::parse_line line.delete("#[a-zA-Z]-").strip
> if (cols[args[0]] != nil) && (cols[args[1]] != nil)
> if cols[args[0]].slice(0,1).to_i > 0 && cols[args[0]].length < 7-
> cols[args[0]].chop.strip
> cols[args[1]].chop.strip
> data[i] = "0"+cols[args[0]]+"-"+cols[args[1]]
> else
> data[i] = cols[args[0]].sub(/[^\w]/,'')+"-"+cols[args[1]]
> end
> i += 1
> end
> 79 end
> 80 return data
>
>
> --
> Grimoire Guru
> SourceMage GNU/Linux
>
>
Rafael it is indeed confusing, I guess I did not understand what you
wanted, why not post some data samples, or maybe it's just me.
For the article no, was my error I have misread your signature it was
"Grégiore" not "Grimoire", well maybe I am just muy cansado, si poedes
tambien ecrirme OFFLIST en castellano si quieres.

cheers
Robert

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

Bob

1/28/2011 2:44:00 PM

0

"Ray Fischer" <rfischer@sonic.net> wrote in message
news:4d426b4c$0$43982$742ec2ed@news.sonic.net...
> criminals <betaxxx@earthlink.net> wrote:
>>
>>http://www.wnd.com/index.php?fa=PAGE.view&pag...
>>
>>Game-changer! Arizona to pass 2012 eligibility law
>>Obama will have to produce birth certificate to run again
>>
>>Posted: January 25, 2011
>>A plan in Arizona to require presidential candidates to prove their
>>eligibility to occupy the Oval Office is approaching critical mass,
>>even though it has just been introduced.
>
> A bunch of racist assholes.

Godwin's Law II - Invoking racism when none is in evidence.


Yoorghis

1/28/2011 3:08:00 PM

0

On Fri, 28 Jan 2011 08:44:17 -0600, "Bob" <dalnetbob@att.net> wrote:

>>>A plan in Arizona to require presidential candidates to prove their
>>>eligibility to occupy the Oval Office is approaching critical mass,
>>>even though it has just been introduced.
>>
>> A bunch of racist assholes.
>
>Godwin's Law II - Invoking racism when none is in evidence.
>

So---Pearce's association with white supremacists isn't
relevant---since he was the one who introduced the legislation in the
State Senate?



>==================================================================================

PAJAMA loves this Guy:


>==================================================

State Rep. Russell Pearce is all excuses when he forwards neo-Nazi
National Alliance e-mails to his supporters, or is seen smiling and
arm-in-arm with local neo-Nazis like J.T. Ready. It's either, "I
didn't read the e-mail closely," or "I didn't know he was a neo-Nazi."
But the proof, as they say, is in the puddin' head. And though in
person, Pearce strikes you as having the I.Q. of a horse's hoof, he
has been extremely effective in instituting his one-note-Johnny
anti-brown plan for Arizona. Last year, it was employer sanctions, the
law that's now driving businesses under, full steam ahead. This year,
the evil gets a little more bold, a little more twisted.

For instance, Pearce has made known his intention to put a referendum
on the November ballot that would prohibit hospitals from issuing
birth certificates to children born of illegal parents. Never mind
that the 14th Amendment to the Constitution clearly states, that "All
persons born or naturalized in the United States, and subject to the
jurisdiction thereof, are citizens of the United States and of the




>========================================================================

Awww----Poor Pajama

Breaking: Hayworth pulls endorsement of Russell Pearce
JD Hayworth issued the following statement today:

Given the regrettable and disturbing nature of the email Russell
Pearce circulated earlier today, I cannot in good conscience lend my
endorsement to his candidacy for State Representative. While Russell
has issued an apology for his email, I nonetheless will not be
associated with any communication that contains anti-Semitic remarks.

The remarks follow an apology by Pearce for e-mailing supporters an
article from a hate group, National Alliance. Pearce explains that he
?liked the first two paragraphs? of the web page and e-mailed it off
before reading the whole thing. He apologized repeatedly for sending
the e-mail and asked recipients to delete it.

Hayworth is listed at the top of Pearce?s endorsement page? let?s see
if Trent Franks follows Hayworth?s lead.


>==========================================================


http://crooksandliars.com/david-neiwert/profiling-arizona-legislat...

Speeders & Drunk Drivers are DEADLY PSYCHOPATHS

1/29/2011 3:27:00 AM

0

On Jan 28, 12:07 am, rfisc...@sonic.net (Ray Fischer) wrote:
> criminals  <beta...@earthlink.net> wrote:
>
> >http://www.wnd.com/index.php?fa=PAGE.view&pag...
>
> >Game-changer! Arizona to pass 2012 eligibility law
> >Obama will have to produce birth certificate to run again
>
> >Posted: January 25, 2011
> >A plan in Arizona to require presidential candidates to prove their
> >eligibility to occupy the Oval Office is approaching critical mass,
> >even though it has just been introduced.
>
> A bunch of racist assholes.


HAHAHA. When the facts and logic are against you, you loony libs
always scream racism!!