Home > Archive > PERL Beginners > January 2008 > creating multiple variables in loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
creating multiple variables in loop
|
|
| Sonal Singhal 2008-01-23, 7:06 pm |
| Simple question, I hope... I am looping through a set of commands, and each
time, I want to create a unique array with a tag representing the loop
number.
for (my $j = 1; $j < $#start_points; $j++){
if ( abs($start_points[0] - $start_points[$j]) > 1999) {
my @{start_points."$n"} = @start_points[$j...$#start_points];
my @{end_points."$n"} = @end_points[$j...$#start_points];
}
}
I would hope this would give me 'n' arrays: @start_points0, @start_points1,
etc. But this isn't working.
ERROR MESSAGE: Can't declare array dereference in my at
/Users/s/Desktop/shorttest.pl line 17, near "} ="
I know I can do an array of arrays but that doesn't work well for the rest
of my program.
Any help would be appreciated!
| |
| Tom Phoenix 2008-01-23, 7:06 pm |
| On Jan 23, 2008 10:27 AM, Sonal Singhal <sonal.singhal1@gmail.com> wrote:
> I know I can do an array of arrays but that doesn't work well for the rest
> of my program.
You should really fix the rest of your program. There is more than one
way to do it, but all the good ways use Perl as it's meant to be used.
Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
| |
| Jenda Krynicky 2008-01-23, 10:07 pm |
| From: "Sonal Singhal" <sonal.singhal1@gmail.com>
> Simple question, I hope... I am looping through a set of commands, and each
> time, I want to create a unique array with a tag representing the loop
> number.
>
> for (my $j = 1; $j < $#start_points; $j++){
> if ( abs($start_points[0] - $start_points[$j]) > 1999) {
> my @{start_points."$n"} = @start_points[$j...$#start_points];
> my @{end_points."$n"} = @end_points[$j...$#start_points];
> }
> }
>
> I would hope this would give me 'n' arrays: @start_points0, @start_points1,
> etc. But this isn't working.
> ERROR MESSAGE: Can't declare array dereference in my at
> /Users/s/Desktop/shorttest.pl line 17, near "} ="
>
> I know I can do an array of arrays but that doesn't work well for the rest
> of my program.
You never want to have a row of variables like this. NEVER. Show us
what "doesn't work well" with an array of arrays.
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Sonal Singhal 2008-01-25, 4:07 am |
| So, what I want to do is go through an existing array and break the array
into mini-arrays at any certain points (where the diff. in two bordering
values are greater than 2000). I programmed it as a recursive function, but
I need to store all these "mini-arrays" and be able to sort them easily by
"mini-array". The problem with the array of arrays is I cannot get it to
treat each row as an array. Maybe if you can help me there...
Also, if I am dynamically allocating my array of arrays, how can I find out
later my index values?
Thanks for the advice thus far.
On Jan 23, 2008 6:21 PM, Jenda Krynicky <Jenda@krynicky.cz> wrote:
> From: "Sonal Singhal" <sonal.singhal1@gmail.com>
> each
> @start_points[$j...$#start_points];
> @start_points1,
> rest
>
> You never want to have a row of variables like this. NEVER. Show us
> what "doesn't work well" with an array of arrays.
>
> Jenda
> ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
> When it comes to wine, women and song, wizards are allowed
> to get drunk and croon as much as they like.
> -- Terry Pratchett in Sourcery
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| Rob Dixon 2008-01-25, 8:06 am |
| Sonal Singhal wrote:
>
> So, what I want to do is go through an existing array and break the array
> into mini-arrays at any certain points (where the diff. in two bordering
> values are greater than 2000). I programmed it as a recursive function, but
> I need to store all these "mini-arrays" and be able to sort them easily by
> "mini-array". The problem with the array of arrays is I cannot get it to
> treat each row as an array. Maybe if you can help me there...
>
> Also, if I am dynamically allocating my array of arrays, how can I find out
> later my index values?
Take a look at the program below. Does it help at all? Does it raise any
more questions?
Rob
use strict;
use warnings;
my @data = 'A' .. 'Z';
my @splitdata;
{
my $subdata;
foreach my $item (@data) {
push @$subdata, $item;
if (@$subdata >= 4) {
push @splitdata, $subdata;
undef $subdata;
}
}
push @splitdata, $subdata if $subdata;
}
foreach my $subdata (@splitdata) {
foreach my $item (@$subdata) {
print "[$item]";
}
print "\n";
}
**OUTPUT**
[A][B][C][D]
[E][F][G][H]
[I][J][K][L]
[M][N][O][P]
[Q][R][S][T]
[U][V][W][X]
[Y][Z]
| |
| Paul Lalli 2008-01-25, 7:06 pm |
| On Jan 25, 12:25=A0am, sonal.singh...@gmail.com (Sonal Singhal) wrote:
> So, what I want to do is go through an existing array and break the array
> into mini-arrays at any certain points (where the diff. in two bordering
> values are greater than 2000). =A0I programmed it as a recursive function,=
but
> I need to store all these "mini-arrays" and be able to sort them easily by=
> "mini-array". =A0The problem with the array of arrays is I cannot get it t=
o
> treat each row as an array. =A0Maybe if you can help me there...
That is non sensical. An "array of arrays" is just an array of
references to other arrays. You use those array references the same
way you use any other array reference.
Have you read:
perldoc perlreftut
and
perldoc perllol
yet?
my @a_of_a =3D ( [ 1, 2, 3], ['a', 'b', 'c'], ['alpha', 'beta',
'gamma'] );
my @nums =3D @{$a_of_a[0]};
my @lets =3D @{$a_of_a[1]};
my @gr =3D @{$a_of_a[2]};
> Also, if I am dynamically allocating my array of arrays, how can I find ou=
t
> later my index values?
I don't understand the question. The index values start at 0 and go
to the size of the array minus 1. What are you actually trying to
accomplish?
If for some reason you think you need to know where in an array some
value (even another array ref) is stored, you're using the wrong data
structure.
Paul Lalli
| |
| Sonal Singhal 2008-01-28, 10:08 pm |
| Thank you for your help! The Perl documents and mini-programs were great.
Cheers,
Sonal
On Jan 24, 2008 9:25 PM, Sonal Singhal <sonal.singhal1@gmail.com> wrote:
> So, what I want to do is go through an existing array and break the array
> into mini-arrays at any certain points (where the diff. in two bordering
> values are greater than 2000). I programmed it as a recursive function, but
> I need to store all these "mini-arrays" and be able to sort them easily by
> "mini-array". The problem with the array of arrays is I cannot get it to
> treat each row as an array. Maybe if you can help me there...
>
> Also, if I am dynamically allocating my array of arrays, how can I find
> out later my index values?
>
> Thanks for the advice thus far.
>
>
> On Jan 23, 2008 6:21 PM, Jenda Krynicky < Jenda@krynicky.cz> wrote:
>
>
|
|
|
|
|