Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Arrays instead of files into hashes

Reply
Thread Tools

Arrays instead of files into hashes

 
 
Francois Massion
Guest
Posts: n/a
 
      01-12-2009
I have code which compares 2 lists of words and churns out the
difference. So far I have done this by reading the content of files
into a hash, but I would like to use 2 arrays, says @array1 and
@array2 instead of files a.txt and b.txt.

I haven't managed to do this successfuly. Any suggestions? Her the
functioning code with the files:

#!/usr/bin/perl -w
use warnings; use strict;
use utf8; #
binmode STDIN, ":utf8"; # input
binmode STDOUT, ":utf8"; # output

my %list2;
open(WORDLIST2,'C:\temp\b.txt') or
die("cannot open 'C:\temp\b.txt' because $! !\n");
while (<WORDLIST2>) {
$list2{$_}=1; #or any other value
}
open(WORDLIST1,'C:\temp\a.txt') or
die("cannot open file1 because $! !\n");
while (<WORDLIST1>){
print unless exists($list2{$_});
}
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      01-12-2009
Francois Massion wrote:

> I have code which compares 2 lists of words and churns out the
> difference. So far I have done this by reading the content of files
> into a hash, but I would like to use 2 arrays, says @array1 and
> @array2 instead of files a.txt and b.txt.
>
> I haven't managed to do this successfuly. Any suggestions? Her the
> functioning code with the files:



The idea of using hashes instead of arrays, is better, why do you want
to change it to arrays? You mean arrays to hold the data that is
usually located in the {a,b}.txt files? Where is that information
being generated from where you could have the data saved into an array,
or are you asking if you can take the data from the files and put them
into arrays? Either way, you shou;dn't want to do that. If you are
using data generated from somewhere, then add that new value to a hash
and see if it exists are you receive the data.

There's no reason to use an array first. If you want to take the data
from the files, just do what you originally were wanting to do, and use
a hash, too. Or do you mean that you want to save the final, unique
values into an array, after you've compared using a hash solution?
Also, you mentioned you wanted to use arrays, but it's not working, so
can you show the code you're trying to use with arrays and what you
expect and what's not working? Sorry, I'm a little confused by what
you're trying to accomplish, so please elaborate.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      01-12-2009
Tim Greer wrote:

> and see if it exists are you receive the data.

^^^^^^^^^^

"as you receive the data"
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-12-2009
Francois Massion wrote:
> I have code which compares 2 lists of words and churns out the
> difference. So far I have done this by reading the content of files
> into a hash, but I would like to use 2 arrays, says @array1 and
> @array2 instead of files a.txt and b.txt.
>
> I haven't managed to do this successfuly. Any suggestions? Her the
> functioning code with the files:
>
> #!/usr/bin/perl -w
> use warnings; use strict;
> use utf8; #
> binmode STDIN, ":utf8"; # input
> binmode STDOUT, ":utf8"; # output
>
> my %list2;
> open(WORDLIST2,'C:\temp\b.txt') or
> die("cannot open 'C:\temp\b.txt' because $! !\n");
> while (<WORDLIST2>) {
> $list2{$_}=1; #or any other value
> }
> open(WORDLIST1,'C:\temp\a.txt') or
> die("cannot open file1 because $! !\n");
> while (<WORDLIST1>){
> print unless exists($list2{$_});
> }


If you already have @array1, you could do the following to
create a hash that has the unique values from @array1.

my %list2 = map { $_ => 1 } @array1;

See also: perldoc -q "How do I compute the difference of two arrays"
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-12-2009
On Mon, 12 Jan 2009 10:53:23 -0800 (PST), Francois Massion <> wrote:

>I have code which compares 2 lists of words and churns out the
>difference. So far I have done this by reading the content of files
>into a hash, but I would like to use 2 arrays, says @array1 and
>@array2 instead of files a.txt and b.txt.
>
>I haven't managed to do this successfuly. Any suggestions? Her the
>functioning code with the files:
>
>#!/usr/bin/perl -w
>use warnings; use strict;
>use utf8; #
>binmode STDIN, ":utf8"; # input
>binmode STDOUT, ":utf8"; # output
>
>my %list2;
>open(WORDLIST2,'C:\temp\b.txt') or
> die("cannot open 'C:\temp\b.txt' because $! !\n");
>while (<WORDLIST2>) {
> $list2{$_}=1; #or any other value
>}
>open(WORDLIST1,'C:\temp\a.txt') or
> die("cannot open file1 because $! !\n");
>while (<WORDLIST1>){
> print unless exists($list2{$_});
>}


This some real old code. True beginner code.
It resolves into the common elements of two arrays.
I know your looking for the difference but you could
extrapolate difference from common. It works for what I
use it for, merging directories, renaming dups.

If you want the whole program for usage exampl just say so.
Good luck!

sln

################################################## #####
# Get Common Elements (from two N-dimensioned Array's)
# IN - Refs to the NxN arrays to compare,
# sort flag and the compare field.
# OUT - Ndx's into Right_Array of matching elements
# ---------------------------------------------------
# Notes -
# 1. Elements are assumed textual and case insensitive
# 2. Ignores in-array duplicates
# 3. Sort will be done if sort flag > 0
#
sub GetCommonElementsNxM($$$$)
{
my ($A_Left,$A_Right,$Srtflg,$Fld) = @_;
$Srtflg = 0 unless defined $Srtflg;
$Fld = 0 unless defined $Fld;
# my @Dup = ();
my @Ndx = ();

if ($Srtflg > 0) {
@{$A_Left} = sort {uc($a->[$Fld]) cmp uc($b->[$Fld])} @{$A_Left};
@{$A_Right} = sort {uc($a->[$Fld]) cmp uc($b->[$Fld])} @{$A_Right};
} else {print "==> Common Elements : Not sorting arrays\n";}

my $rpos = 0;
my $rend = @{$A_Right};
my $cnt = 0;
my $llast = undef;
my $rlast = undef;
foreach my $left_element (@{$A_Left})
{
next if (uc($left_element->[$Fld]) eq uc($llast->[$Fld]));

$rpos += $cnt;
$cnt = 0;
foreach my $right_element (@{$A_Right}[$rpos..($rend-1)])
{
last if (uc($left_element->[$Fld]) lt uc($right_element->[$Fld]));
$cnt++;
next if (uc($right_element->[$Fld]) eq uc($rlast->[$Fld]));
if (uc($left_element->[$Fld]) eq uc($right_element->[$Fld]))
{
# push (@Dup, $right_element->[$Fld]); # the string
push (@Ndx, $rpos+$cnt-1); # the index into R_Array
last;
}
$rlast = $right_element;
}
$llast = $left_element;
last if ($rpos >= $rend);
}
# return (@Dup);
return (@Ndx);
}


################################################## #####
# Get Common Elements from single Array's
# IN - Refs to the Nx1 arrays to compare, sort flag
# OUT - Ndx's into Right_Array of matching elements
# ---------------------------------------------------
# Notes -
# 1. Elements are assumed textual and case insensitive
# 2. Ignores in-array duplicates
# 3. Sort will be done if sort flag > 0
################################################## #####
sub GetCommonElements($$$)
{
my ($A_Left,$A_Right,$Srtflg) = @_;
$Srtflg = 0 unless defined $Srtflg;
# my @Dup = ();
my @Ndx = ();

if ($Srtflg > 0) {
@{$A_Left} = sort {uc($a) cmp uc($b)} @{$A_Left};
@{$A_Right} = sort {uc($a) cmp uc($b)} @{$A_Right};
} else {print "==> Common Elements : Not sorting arrays\n";}

my $rpos = 0;
my $rend = @{$A_Right};
my $cnt = 0;
my $llast = '';
my $rlast = '';
foreach my $left_element (@{$A_Left})
{
next if (uc($left_element) eq uc($llast));

$rpos += $cnt;
$cnt = 0;
foreach my $right_element (@{$A_Right}[$rpos..($rend-1)])
{
last if (uc($left_element) lt uc($right_element));
$cnt++;
next if (uc($right_element) eq uc($rlast));
if (uc($left_element) eq uc($right_element))
{
# push (@Dup, $right_element); # the string
push (@Ndx, $rpos+$cnt-1); # the index into R_Array
last;
}
$rlast = $right_element;
}
$llast = $left_element;
last if ($rpos >= $rend);
}
# return (@Dup);
return (@Ndx);
}

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-12-2009
Francois Massion <> wrote:

> I would like to use 2 arrays, says @array1 and
> @array2 instead of files a.txt and b.txt.



> while (<WORDLIST2>) {
> $list2{$_}=1; #or any other value
> }



my @b_txt = <WORDLIST2>;
close WORDLIST2;
foreach ( @b_txt ) {
$list2{$_} = 1;
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-13-2009
On Mon, 12 Jan 2009 17:55:36 -0600, Tad J McClellan <> wrote:

>Francois Massion <> wrote:
>
>> I would like to use 2 arrays, says @array1 and
>> @array2 instead of files a.txt and b.txt.

>
>
>> while (<WORDLIST2>) {
>> $list2{$_}=1; #or any other value
>> }

>
>
> my @b_txt = <WORDLIST2>;

Isin't slurp a bit rich?
[snip]

sln

 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      01-13-2009
wrote:

> On Mon, 12 Jan 2009 17:55:36 -0600, Tad J McClellan
> <> wrote:
>
>>Francois Massion <> wrote:
>>
>>> I would like to use 2 arrays, says @array1 and
>>> @array2 instead of files a.txt and b.txt.

>>
>>
>>> while (<WORDLIST2>) {
>>> $list2{$_}=1; #or any other value
>>> }

>>
>>
>> my @b_txt = <WORDLIST2>;

> Isin't slurp a bit rich?
> [snip]
>
> sln


Probably, but that's what the OP asked for. I can't imagine why, but
perhaps they just need to elaborate on their reasons to get a good
answer. Personally, I didn't answer them, because I had to ask what
they were wanting to do, since it didn't seem clear to me (or didn't
seem to have a purpose and would just waste good processing).
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-13-2009
On Mon, 12 Jan 2009 16:41:12 -0800, Tim Greer <> wrote:

> wrote:
>
>> On Mon, 12 Jan 2009 17:55:36 -0600, Tad J McClellan
>> <> wrote:
>>
>>>Francois Massion <> wrote:
>>>
>>>> I would like to use 2 arrays, says @array1 and
>>>> @array2 instead of files a.txt and b.txt.
>>>
>>>
>>>> while (<WORDLIST2>) {
>>>> $list2{$_}=1; #or any other value
>>>> }
>>>
>>>
>>> my @b_txt = <WORDLIST2>;

>> Isin't slurp a bit rich?
>> [snip]
>>
>> sln

>
>Probably, but that's what the OP asked for. I can't imagine why, but
>perhaps they just need to elaborate on their reasons to get a good
>answer. Personally, I didn't answer them, because I had to ask what
>they were wanting to do, since it didn't seem clear to me (or didn't
>seem to have a purpose and would just waste good processing).


There's a thin line between yes and no, but if its not yes....

sln
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      01-13-2009
wrote:

> On Mon, 12 Jan 2009 16:41:12 -0800, Tim Greer <>
> wrote:
>
>> wrote:
>>
>>> On Mon, 12 Jan 2009 17:55:36 -0600, Tad J McClellan
>>> <> wrote:
>>>
>>>>Francois Massion <> wrote:
>>>>
>>>>> I would like to use 2 arrays, says @array1 and
>>>>> @array2 instead of files a.txt and b.txt.
>>>>
>>>>
>>>>> while (<WORDLIST2>) {
>>>>> $list2{$_}=1; #or any other value
>>>>> }
>>>>
>>>>
>>>> my @b_txt = <WORDLIST2>;
>>> Isin't slurp a bit rich?
>>> [snip]
>>>
>>> sln

>>
>>Probably, but that's what the OP asked for. I can't imagine why, but
>>perhaps they just need to elaborate on their reasons to get a good
>>answer. Personally, I didn't answer them, because I had to ask what
>>they were wanting to do, since it didn't seem clear to me (or didn't
>>seem to have a purpose and would just waste good processing).

>
> There's a thin line between yes and no, but if its not yes....
>
> sln


Its (its) or it's (it is) not yes, and what isn't? I'd say it is yes
(being it's "probably" going to be more resource intensive to grab all
of the contents of WORLDLIST2 into an array), only to compare then,
comparing it to a while loop to see if a hash value exists before
adding it (if the OP wants to add it). I absolutely don't understand
your cryptic response. I'm certain you were being sarcastic, but I
don't know if you mean to have a question in there.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a select and map for hashes instead of arrays? Jeff Ruby 5 09-18-2007 10:17 PM
Combining three arrays of hashes into one? Keith Lee Perl Misc 8 02-22-2006 12:53 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Joining 2 arrays into hashes Edward Wijaya Perl Misc 20 06-03-2004 11:39 PM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57