LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 02-09-2010, 02:05 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
How to restrict the perl array maximum size?


Hi folks,
How to restrict the maximum size of an array variable in perl such that I should not be able to have array elements beyond the maximum size?
 
Old 02-09-2010, 04:30 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
Quote:
Originally Posted by ashok.g View Post
Hi folks,
How to restrict the maximum size of an array variable in perl such that I should not be able to have array elements beyond the maximum size?
$string =~ s/^(.*{your_nb_of_char}).*/$1/g;

Ex: $string =~ s/^(.*{10}).*$/$1/g;

create a loop for each element of your array

Code:
for($i = 0; $i < $#myArray; $i++) {
	$myArray[$i] =~ s/^(.*{10}).*$/$1/g; #Will keep only the 10 first characters of your elements in you array
}
Angel.
 
Old 02-09-2010, 04:54 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by angel115 View Post
Code:
for($i = 0; $i < $#myArray; $i++) {
	$myArray[$i] =~ s/^(.*{10}).*$/$1/g; #Will keep only the 10 first characters of your elements in you array
}
Angel.
I think you didn't get my question.
I want the total no. of elements to be restricted rather than the size of each element. Got it Angel?
 
Old 02-09-2010, 05:22 AM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
oups
 
Old 02-09-2010, 05:22 AM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
Ah ok,

I get it. But depending of what you want to do you can proceed differently.
For info you can't predefine the max numb of element that your array will hold.

but what you can do for example is checking how many element your array have before adding it a new element:

Ex:
Code:
if ($#myArray > 6) {
	print "we can't add this value to this array.";
}
else {
	push(@myArray, "My new value");
}
Does this can fit your need?

if not please explain in more detail the context where you want to implement this.

Best regards,
Angel.

Last edited by angel115; 02-09-2010 at 05:24 AM.
 
Old 02-09-2010, 06:06 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Actually, I am expecting a predefined function to serve this purpose like if am trying to access an array element which will reside beyod my limit I should be warned.

Anyway,Thanks for your reply.
Happy Posting... Bye.....

Last edited by ashok.g; 02-09-2010 at 06:11 AM.
 
Old 02-09-2010, 07:30 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by ashok.g View Post
Actually, I am expecting a predefined function to serve this purpose like if am trying to access an array element which will reside beyod my limit I should be warned.

Anyway,Thanks for your reply.
Happy Posting... Bye.....
Perl's arrays are deliberately without limits of this kind. In most cases, it makes them much easier to work with.

So, I don't know of a predefined function that will work the way you want - at least, not one predefined by Perl itself. However, you can always create your own function if you like and set whatever limits you want there.

Let me ask a different question though: why do you think you need this? What sorts of cases are you worried about, or what problems are you running into? I suspect you think you need a limitation that you don't really need.
 
Old 02-09-2010, 09:37 AM   #8
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
Maybe he want to do somthing like a buffer which will keep only the "n" last result...

in this case he could do this:
Code:
&buffer($result);

sub buffer
{
	($newvalue) = @_;
	if ($#myArray > 6) {
		shift (@myArray);
		push(@myArray, "$newvalue");
	}
	else {
		push(@myArray, "$newvalue");
	}
}
Angel
 
  


Reply

Tags
array, perl, restrict, size


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
To restrict maximum download file size in Squid proxy satishap Linux - Security 3 05-23-2008 03:09 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 02:47 AM
Maximum 4 dimensional array size disruptive Programming 26 02-06-2007 11:51 PM
Maximum file size Hiran Joseph Programming 7 11-24-2006 08:54 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 06:43 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