LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to restrict the perl array maximum size? (http://www.linuxquestions.org/questions/programming-9/how-to-restrict-the-perl-array-maximum-size-787923/)

ashok.g 02-09-2010 01:05 AM

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?

angel115 02-09-2010 03:30 AM

Quote:

Originally Posted by ashok.g (Post 3857494)
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.

ashok.g 02-09-2010 03:54 AM

Quote:

Originally Posted by angel115 (Post 3857587)
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?

angel115 02-09-2010 04:22 AM

oups

angel115 02-09-2010 04:22 AM

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.

ashok.g 02-09-2010 05:06 AM

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.....

Telemachos 02-09-2010 06:30 AM

Quote:

Originally Posted by ashok.g (Post 3857664)
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.

angel115 02-09-2010 08:37 AM

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

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


All times are GMT -5. The time now is 05:23 AM.