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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-20-2012, 08:30 AM
|
#1
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Rep:
|
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.
|
|
|
|
11-20-2012, 09:35 AM
|
#2
|
|
Member
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224
Rep:
|
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.
|
11-20-2012, 02:55 PM
|
#3
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by goossen
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.
|
11-21-2012, 01:15 AM
|
#4
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Original Poster
Rep:
|
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.
|
|
|
|
11-21-2012, 05:41 AM
|
#5
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by qrange
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.
|
|
|
|
11-21-2012, 06:06 AM
|
#6
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Original Poster
Rep:
|
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).
|
|
|
|
11-21-2012, 06:39 AM
|
#7
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by qrange
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
- 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.
|
11-21-2012, 07:03 AM
|
#8
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Original Poster
Rep:
|
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.
|
|
|
|
11-21-2012, 07:09 AM
|
#9
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
Quote:
Originally Posted by qrange
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.
|
11-21-2012, 07:38 AM
|
#10
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by qrange
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.
|
11-21-2012, 08:15 AM
|
#11
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Original Poster
Rep:
|
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.
|
|
|
|
11-21-2012, 09:39 AM
|
#12
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by qrange
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.
|
11-21-2012, 10:21 AM
|
#13
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by qrange
... 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 ?
|
|
|
|
11-21-2012, 12:21 PM
|
#14
|
|
Member
Registered: Jul 2006
Location: Belgrade, Serbia
Distribution: Debian
Posts: 571
Original Poster
Rep:
|
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.
|
|
|
|
11-21-2012, 12:47 PM
|
#15
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
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 12:51 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|