LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-20-2012, 09:30 AM   #1
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Rep: Reputation: 29
perl check array


I need to check if a numerical array is subset of larger one.
what would be simplest way to do it?
thanks.
 
Old 11-20-2012, 10:35 AM   #2
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
It's included on the FAQ section of the perl documentation. My advise is to use a Hash table.

Do you have something done ? If yes, you can post here to have a look.
 
1 members found this post helpful.
Old 11-20-2012, 03:55 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by goossen View Post
It's included on the FAQ section of the perl documentation. My advise is to use a Hash table.

Do you have something done ? If yes, you can post here to have a look.
Your advice/approach is conceptually and in many cases practically correct.

However, for big datasets hash might be too memory consuming, so in such a case sorting of arrays first and then application of trivial common-difference algorithm to the sorted arrays might be the practical way to go.
 
1 members found this post helpful.
Old 11-21-2012, 02:15 AM   #4
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Original Poster
Rep: Reputation: 29
thanks for the replies. I had searched Google and this forum, and tried this:
Code:
 foreach (@columns) {
	if (not exists $shash{$_}) {print"Error in $_ ! \n";}
 }

but for some reason it doesn't work as expected. dataset is very small. elements are sorted, order is not relevant.
 
Old 11-21-2012, 06:41 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by qrange View Post
thanks for the replies. I had searched Google and this forum, and tried this:
Code:
 foreach (@columns) {
	if (not exists $shash{$_}) {print"Error in $_ ! \n";}
 }

but for some reason it doesn't work as expected. dataset is very small. elements are sorted, order is not relevant.
What did you do debug the problem ? Debugging is finding the very first place in your code in which the code doesn't work as expected.

The code you published doesn't make sense - because it's partial code, not full one.

Do you have

Code:
use strict;
use warnings;
in the beginning of your code ?

Why did you mention that the elements are sorted ? For the approach with hash they needn't be sorted.
 
Old 11-21-2012, 07:06 AM   #6
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Original Poster
Rep: Reputation: 29
thing is, it partially works. I have 'use warning;' but not 'use strict;'

trying to check if array is subset of this: (2,3,10,7,11,15,16,19,20,21,22,25,26)
so (10,20,25) should be ok and (14,23,26) not. but when I test with entire set, it reports error for some elements (which should be fine).
 
Old 11-21-2012, 07:39 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by qrange View Post
thing is, it partially works. I have 'use warning;' but not 'use strict;'

trying to check if array is subset of this: (2,3,10,7,11,15,16,19,20,21,22,25,26)
so (10,20,25) should be ok and (14,23,26) not. but when I test with entire set, it reports error for some elements (which should be fine).
Put

Code:
use strict;
- there is no sense for us here spending time on possible mistakes to be automatically caught by 'use strict;'.

As I said, it doesn't make sense looking at partial code. The partial code you've published can not work by definition. It doesn't necessarily mean the code is wrong, simply an important part may be missing, and I do not know whether it exists in the full code.
 
1 members found this post helpful.
Old 11-21-2012, 08:03 AM   #8
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Original Poster
Rep: Reputation: 29
if I use strict the damn thing won't run at all. it doesn't even come close to problem. anyway, thanks, I'll try to sort out minor issues first.
 
Old 11-21-2012, 08:09 AM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by qrange View Post
thanks for the replies. I had searched Google and this forum, and tried this:
Code:
 foreach (@columns) {
	if (not exists $shash{$_}) {print"Error in $_ ! \n";}
 }

but for some reason it doesn't work as expected. dataset is very small. elements are sorted, order is not relevant.
The code looks good, it works very well. The only thing you're missing is that the hash must be populated somewhere before these lines of code...

Markus
 
1 members found this post helpful.
Old 11-21-2012, 08:38 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by qrange View Post
if I use strict the damn thing won't run at all. it doesn't even come close to problem. anyway, thanks, I'll try to sort out minor issues first.
Because you are making mistakes.

Because you just copy-paste code without trying to understand how it should work.

Did you read 'perldoc -f exists' ?

I.e. do you understand why and how the code is supposed to work ? Here is an analogy for you: you are at a class to study arithmetic. Instead of trying to understand and learn how to perform calculations you are frantically pressing calculator buttons, and the calculator refuses to work - because there is no battery in it, and you don't even understand it needs some power source.
 
1 members found this post helpful.
Old 11-21-2012, 09:15 AM   #11
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Original Poster
Rep: Reputation: 29
relax man..
anyway I figured it out. I now know why hashes can't have odd number of elements.
initial try was this:
my %shash = (2,3,10,7,11,15,16,19,20,21,22,25,26);

so I tried:
my %shash = (1,2,2,3,3,10,4,7,5,6,5,15,7,16,8,19,9,20,10,21,11,22,12,25,13,26);

and ended up with:
my %shash = (2,1,3,1,10,1,7,1,11,1,15,1,16,1,19,1,20,1,21,1,22,1,25,1,26,1);

which so far seems to work fine.
about the strict. the only problem was that bunch of variables weren't declared properly, it couldn't help me with hashes.
I should reduce number of variables.

thanks all.
 
Old 11-21-2012, 10:39 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by qrange View Post
relax man..
anyway I figured it out. I now know why hashes can't have odd number of elements.
initial try was this:
my %shash = (2,3,10,7,11,15,16,19,20,21,22,25,26);

so I tried:
my %shash = (1,2,2,3,3,10,4,7,5,6,5,15,7,16,8,19,9,20,10,21,11,22,12,25,13,26);

and ended up with:
my %shash = (2,1,3,1,10,1,7,1,11,1,15,1,16,1,19,1,20,1,21,1,22,1,25,1,26,1);

which so far seems to work fine.
about the strict. the only problem was that bunch of variables weren't declared properly, it couldn't help me with hashes.
I should reduce number of variables.

thanks all.
Quote:
the only problem was that bunch of variables weren't declared properly
- sorry, but you make claims as if know something though in fact you don't.

First and foremost, 'use strict;' changes semantics. You need to understand what lexical scope is ( https://en.wikipedia.org/wiki/Scope_...ter_science%29 ) and what dynamic scope is ( https://en.wikipedia.org/wiki/Scope_...ynamic_scoping ).

'use strict;' also allows you to catch things like

Code:
my $filename = 'foo.txt';
...
open(my $fh, '<', $file_name) # $file_name is undefined, it should have been $filename instead

Quote:
I now know why hashes can't have odd number of elements
- this is a very revealing statement. In translation from English into Simple it means: "I started using hashes not even bothering to read what they are beforehand". The same situation as with 'exists', isn't it ?


Quote:
so I tried:
Code:
my %shash = (1,2,2,3,3,10,4,7,5,6,5,15,7,16,8,19,9,20,10,21,11,22,12,25,13,26);
- of course, it is nonsense - just because you didn't bother to read what hashes are and what 'exists' does, or did read ?

And, by the way, Perl has a nice syntax sugar for hashes:

Code:
my %hash =
  (
  one => 1,
  two => 2,
  ...
  );
.

As your responses show, you didn't make a minimal decency effort to learn first, but started from searching the web for a piece of code.

Or I am wrong, you've started from http://perldoc.perl.org/ and grabbed all your code and approaches from there ?
 
1 members found this post helpful.
Old 11-21-2012, 11:21 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by qrange View Post
... elements are sorted, order is not relevant.
And how do you know elements are sorted ? I am addressing the issue after looking at your code snippets.

I.e. how do you know that Perl uses hashes with sorted keys ? And at all hashes with ordered keys ? Where did you read about it ? Do you think hashes with ordered keys are the simplest kind of hashes ?
 
Old 11-21-2012, 01:21 PM   #14
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian
Posts: 748

Original Poster
Rep: Reputation: 29
um, are all those questions rhetorical ?
I was referring to array, its elements are sorted, hash is a 'constant' (if that makes any sense).
yes, I don't know much about arrays, hashes, or Perl in general. that's why I asked the question.
basically I try various things, modify examples, solve problems with help of google.
I should have read about hashes, exist and other stuff before posting, sorry.
 
Old 11-21-2012, 01:47 PM   #15
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hi,

your code
Code:
my %shash = (2,1,3,1,10,1,7,1,11,1,15,1,16,1,19,1,20,1,21,1,22,1,25,1,26,1);
may work. But why do you need a program for this (one can see if one array is a subset of the other)? or how do you create the hash when the array has 10000 elements?

Markus

Last edited by markush; 11-21-2012 at 01:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl email validation tld check mx check swanny99 Programming 1 06-27-2012 09:42 PM
Perl .check if data are exist in the array before adding new data ufmale Programming 12 07-14-2010 06:26 AM
How to check if an array is empty in Perl (newbie question) resetreset Programming 4 11-28-2008 04:26 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 02:47 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM


All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration