Click here to get back home

$# array creation and data rotation

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
$# array creation and data rotation Hobart Duncan 06-01-2008
Posted by Hobart Duncan on June 1, 2008, 5:28 pm
Please log in for more thread options
This newbie need some technical information:

....after the creation of an array of (just for grins..) 10 integers read in
from (more grins....) the serial port, what is the proper way to maintain the
array to ONLY 10 values?

I want to first create an array: .... $#Test = sprintf (%d, 100/10)

now, as data comes in via the serial port (I keeping everything in decimal) I
first want to "shift" the data: shift (@Test)

and then push the new data: push (@Test, newdata).

After the new data is inserted, I perform calculations (averages, etc) and then
move on..... until the next loop when the shift and push are executed again.

Because I am not using the shifted data, does the shift statement just pull the
data and throw it away? .... and is this a safe way to rotate the data within
the array?

Tnx, in advance....
Hobart

Posted by Jens Thoms Toerring on June 1, 2008, 5:52 pm
Please log in for more thread options
> ....after the creation of an array of (just for grins..) 10 integers read
> in from (more grins....) the serial port, what is the proper way to
> maintain the array to ONLY 10 values?

> I want to first create an array: .... $#Test = sprintf (%d, 100/10)

That creates an array with 11 undefined elements. I though you wanted
an array with 10 elements, didn't you? Remember '$#Test' is the index
of the last element and you set it to 10. And the index of the first
element is 0.

> now, as data comes in via the serial port (I keeping everything in
> decimal) I first want to "shift" the data: shift (@Test) and then push the
> new data: push (@Test, newdata).

> After the new data is inserted, I perform calculations (averages, etc) and
> then move on..... until the next loop when the shift and push are executed
> again.

> Because I am not using the shifted data, does the shift statement just
> pull the data and throw it away? .... and is this a safe way to rotate the
> data within the array? >

Yes, it's completely ok. shift() removes the first element from the
array and returns it as it's return value. You can do with that value
whatever you like, throwing it away by mot assigning it to anything is
one of your options. And push() appends a new value (or, if you pass
it more than a single value, all those values) to the array. Also here
everything you claim to do looks fine.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by sln on June 6, 2008, 1:17 pm
Please log in for more thread options

>This newbie need some technical information:
>
>....after the creation of an array of (just for grins..) 10 integers read in
>from (more grins....) the serial port, what is the proper way to maintain the
>array to ONLY 10 values?
>
>I want to first create an array: .... $#Test = sprintf (%d, 100/10)
>
>now, as data comes in via the serial port (I keeping everything in decimal) I
>first want to "shift" the data: shift (@Test)
>
>and then push the new data: push (@Test, newdata).
>
>After the new data is inserted, I perform calculations (averages, etc) and then
>move on..... until the next loop when the shift and push are executed again.
>
>Because I am not using the shifted data, does the shift statement just pull the
>data and throw it away? .... and is this a safe way to rotate the data within
>the array?
>
>Tnx, in advance....
>Hobart

You could do a little more fancy ring buffer:
====================

use strict;
use warnings;

my $head = 0;
my $tail = 0;
my $bufsize = 10;
my @ringbuffer = ();


my ($i, $val);
for ($i = 0; $i < 26; $i++)
{
        if (!AddHead ( $i )) {
                print "Buffer will overflow, take some off first\n";
                while (RemoveTail( $val )) {
                        print "val= $val\n";
                }
                $i--;
        }
}
print "Cleaning out buffer\n";
while (RemoveTail( $val )) {
        print "val= $val\n";
}


sub AddHead
{
        my $tmp = $head+1;
        $tmp = 0 if ($tmp > $bufsize);
        return 0 if ($tmp == $tail);
        $head = $tmp;
        $ringbuffer[$head] = shift;
        return 1;
}
sub RemoveTail
{
        my $refval = shift;
        die "RemoveTail(): Parameter required.\n" if(!defined($refval));
        return 0 if ($head == $tail);
        $tail++;
        $tail = 0 if ($tail > $bufsize);
        $$refval = $ringbuffer[$tail];
        return 1;
}

===========
sln



Similar ThreadsPosted
threads and logfile rotation July 30, 2007, 12:56 pm
How do I get the data out of this array? June 4, 2007, 11:11 pm
Transpose data into a 2 dimensional array. August 2, 2004, 4:00 pm
delimited data into nested array August 4, 2004, 1:49 am
Data Structure help (hash/array) May 7, 2007, 2:23 pm
Printing and Formatting data from hash (or array) October 30, 2006, 11:31 am
Newbie question: load data to array September 16, 2007, 11:20 am
PERL OOP newbie question: What is the best way to handle array data? May 1, 2007, 2:22 am
XS - Variable creation February 15, 2006, 10:59 am
Table creation March 21, 2008, 10:22 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap