push() to @array[$count] gives error, is there any other way?
push() to @array[$count] gives error, is there any other way?
am 23.12.2005 18:46:36 von kelly
#!/usr/bin/perl
....
....
....
for ($count=0; $count<2; $count++)
{
# creation of array names array0, array1.....
push(@arraystrholder,join("array","",$count));
# be sure there's no hidden characters
chomp(@arraystrholder);
print "\n";
# push all string entries to created array
for ($counta=1; $counta<$argnumplus; $counta++)
{
# $ARGV is something like this... abc abc, aa, bb cc dder3,
push(@arraystrholder[0],$ARGV[$counta]); ---->>>>> problem here
}
}
error:
Type of arg 1 to push must be array (not array slice)
My comment:
But that's what I need to do. My plan is to push every string from ARGV
to array1, array2...etc...
array1 will hold the first strings until comma (,) is detected
so array 1 will be @array1 = abc abc,
@array2 = aa,
@array3 = bb cc dder3,
The array names are dynamically created since I don't know how much
entries, or commas, the user will input.
Could anyone give me an algorithm that I could follow on how to take
this, if the solution will be too complicated to write in perl?
I'm stuck and I can't think of anything else on how to continue, I would
have to start all over again, if I can't think of how to continue.
Thanks.
Re: push() to @array[$count] gives error, is there any other way?
am 23.12.2005 20:15:19 von Paul Lalli
kelly wrote:
> #!/usr/bin/perl
> ...
> ...
> ...
Please don't do that. Post a *real* piece of code that people can run
by copy and pasting it.
> for ($count=0; $count<2; $count++)
> {
> # creation of array names array0, array1.....
That should be your first clue that you're doing something wrong.
Whenever you feel the need to create a list of variables all with the
same name, you want to use an array instead. In this case, since the
variables you want to create *are* arrays, you want to use an array of
arrays.
> push(@arraystrholder,join("array","",$count));
What do you believe that's doing? This would join the empty string and
the value $count using the string 'array'. That is, it would push the
string "array$count" onto @arraystrholder. Is that your intent? If
so, why the needless joining? Just do it directly:
push @arraystrholder, "array$count";
>
> # be sure there's no hidden characters
> chomp(@arraystrholder);
The comment does not reflect the code. The code is removing a trailing
newline from every element of @arraystrholder that has a trailing
newline.
I don't even know what you mean by "hidden characters"
> print "\n";
>
> # push all string entries to created array
> for ($counta=1; $counta<$argnumplus; $counta++)
> {
> # $ARGV is something like this... abc abc, aa, bb cc dder3,
No, $ARGV is nothing like that. Perhaps you mean that @ARGV contains
those strings? It would be far better if you showed us, via Perl, what
you actually have in your arrays, rather than attempting to explain it
in words.
> push(@arraystrholder[0],$ARGV[$counta]); ---->>>>> problem here
> }
> }
>
> error:
> Type of arg 1 to push must be array (not array slice)
>
> My comment:
> But that's what I need to do.
No, that's not at all what you need to do. You cannot "push" onto a
scalar. That just doesn't make any sense.
> My plan is to push every string from ARGV
> to array1, array2...etc...
> array1 will hold the first strings until comma (,) is detected
> so array 1 will be @array1 = abc abc,
> @array2 = aa,
> @array3 = bb cc dder3,
Again, you should be using an array of arrays for this, not a bunch of
individual arrays with similar names.
> The array names are dynamically created since I don't know how much
> entries, or commas, the user will input.
Using an array of arrays will elminate this problem.
> Could anyone give me an algorithm that I could follow on how to take
> this, if the solution will be too complicated to write in perl?
Blaming the language for your programmatic and algorithmic errors is a
rather childish response.
> I'm stuck and I can't think of anything else on how to continue, I would
> have to start all over again, if I can't think of how to continue.
You have not well defined your problem at all. If I understand it
correctly, you want to parse the command line, pushing each element
into an array, advancing to a new array each time the current command
line argument ends in a newline. Is that correct?
#!/usr/bin/perl
use strict;
use warnings;
my @array_of_arrays;
my $array_position = 0;
for my $arg (@ARGV){
if ($arg =~ /,$/) { #if argument ends in a comma
chop $arg; #remove the comma;
push @{$array_of_arrays[$array_position]}, $arg;
$array_position++;
} else {
push @{$array_of_arrays[$array_position]}, $arg;
}
}
#print data out, just to check:
for my $i (0..$#array_of_arrays) {
print "Array $i: @{$array_of_arrays[$i]}\n";
}
__END__
$ ./clpm.pl foo bar, baz, biff bam boom, done
Array 0: foo bar
Array 1: baz
Array 2: biff bam boom
Array 3: done
Read more about multi-dimensional structures (ie, arrays of arrays) in:
perldoc perllol
perldoc perldsc
Paul Lalli
Re: push() to @array[$count] gives error, is there any other way?
am 24.12.2005 11:45:58 von Joe Smith
kelly wrote:
> for ($count=0; $count<2; $count++)
for my $count (0 .. 1) { ... } # Avoid using C-style for(;;)
> # push all string entries to created array
> for ($counta=1; $counta<$argnumplus; $counta++)
> {
> # $ARGV is something like this... abc abc, aa, bb cc dder3,
> push(@arraystrholder[0],$ARGV[$counta]); ---->>>>> problem here
> }
In C, argv[0] = program name; argv[1] = first argument.
In perl, $0 = program name; $ARGV[0] = first argument.
You need to start at zero, not one.
> Type of arg 1 to push must be array (not array slice)
for my $n (0 .. 3) {
push @{$arraystrholder[$n]},@ARGV;
}
If $arraystrholder[$n] is an array reference, then
@{$arraystrholder[$n]} is the array it references
(and @arraystrholder is an array of array references).
Is that what you're trying to accomplish?
> My plan is to push every string from ARGV
> to array1, array2...etc...
> array1 will hold the first strings until comma (,) is detected
> so array 1 will be @array1 = abc abc,
> @array2 = aa,
> @array3 = bb cc dder3,
my @groups; # Array of array references
my $n = 0; # Index into @strings;
foreach (@ARGV) {
push @{$groups[$n]},$_; # Add each one to current array
$n++ if /,$/; # Start new array when comma seen
}
> The array names are dynamically created since I don't know how much
> entries, or commas, the user will input.
You don't want array names to do that.
You should use an array of array references (or "list of lists").
What I've shown above is @groups being an array of array references
and @{$groups[$n]} is an array holding the all the items in
group $n.
> if the solution will be too complicated to write in perl?
The solution is not too complicated for perl. It is very
straightforward once you learn the concept of "list of lists".
-Joe
Re: push() to @array[$count] gives error, is there any other way?
am 24.12.2005 18:49:15 von Justin C
On 2005-12-24, Joe Smith wrote:
>
> The solution is not too complicated for perl. It is very
> straightforward once you learn the concept of "list of lists".
I had real trouble with these until I started reading The Camel. It's
covered very early and even I understood it. Once I'd found "array of
arrays" I found I was using them nearly all the time.
Note for OP, the book (Programming Perl) is, I know, expensive, but I
also find it invaluable - a good follow up to The Llama (or Learning
Perl).
Justin.
--
Justin C, by the sea.
Re: push() to @array[$count] gives error, is there any other way?
am 24.12.2005 23:50:10 von Paul Lalli
Justin C wrote:
> On 2005-12-24, Joe Smith wrote:
> >
> > The solution is not too complicated for perl. It is very
> > straightforward once you learn the concept of "list of lists".
>
> I had real trouble with these until I started reading The Camel. It's
> covered very early and even I understood it. Once I'd found "array of
> arrays" I found I was using them nearly all the time.
>
> Note for OP, the book (Programming Perl) is, I know, expensive, but I
> also find it invaluable - a good follow up to The Llama (or Learning
> Perl).
"Programming Perl" is not really a follow up to "Learning Perl". The
first is a reference, the second is a tutorial. An actual follow up to
"Learning Perl" is "Learning Perl Objects, References and Modules",
which is a direct sequel to "Learning Perl", picking up the tutorial
right where "Learning Perl" leaves off.
"Programming Perl" is a dead-tree version of the various perldocs. If
you're concerned about the finances of buying the book, allow me to
suggest you can get nearly the same information from
http://perldoc.perl.org or from the `perldoc` program installed with
your Perl distribution.
(In this particular case, the relevant perldocs are:
perldoc perlreftut
perldoc perllol
perldoc perldsc
perldoc perlref
and, perhaps:
perldoc perltoot
perldoc perlobj
perldoc perlmod
perldoc perlbot
)
Paul Lalli
Re: push() to @array[$count] gives error, is there any other way?
am 25.12.2005 01:03:37 von Justin C
On 2005-12-24, Paul Lalli wrote:
> Justin C wrote:
>> On 2005-12-24, Joe Smith wrote:
>> >
>> > The solution is not too complicated for perl. It is very
>> > straightforward once you learn the concept of "list of lists".
>>
>> I had real trouble with these until I started reading The Camel. It's
>> covered very early and even I understood it. Once I'd found "array of
>> arrays" I found I was using them nearly all the time.
>>
>> Note for OP, the book (Programming Perl) is, I know, expensive, but I
>> also find it invaluable - a good follow up to The Llama (or Learning
>> Perl).
>
> "Programming Perl" is not really a follow up to "Learning Perl".
I didn't mean that it *is* a followup, what I mean is, it's not
unreasonable to read the one after the other.
> An actual follow up to
> "Learning Perl" is "Learning Perl Objects, References and Modules",
> which is a direct sequel to "Learning Perl", picking up the tutorial
> right where "Learning Perl" leaves off.
There is a follow-up to the Llama?! Will have to look that up... I
really found the tutorial section a great aid to my learning.
> "Programming Perl" is a dead-tree version of the various perldocs. If
> you're concerned about the finances of buying the book, allow me to
> suggest you can get nearly the same information from
> http://perldoc.perl.org or from the `perldoc` program installed with
> your Perl distribution.
I do like being able to just flick through and see what catches my eye
from time to time, one advantage a book has over the net... WRT dead
trees, I hope OReilly are only printing onto paper from sustainable
sources...
Justin.
--
Justin C, by the sea.
Re: push() to @array[$count] gives error, is there any other way?
am 31.12.2005 11:06:58 von Dave Cross
On Sun, 25 Dec 2005 00:03:37 +0000, Justin C wrote:
> There is a follow-up to the Llama?! Will have to look that up... I
> really found the tutorial section a great aid to my learning.
You might like to wait a few weeks. The new edition of "Learning Perl
Objects, References and Modules" - which has been renamed to "Intermediate
Perl" will be released early in 2006.
Dave...
Re: push() to @array[$count] gives error, is there any other way?
am 31.12.2005 16:21:53 von Justin C
On 2005-12-31, Dave Cross wrote:
> On Sun, 25 Dec 2005 00:03:37 +0000, Justin C wrote:
>
>> There is a follow-up to the Llama?! Will have to look that up... I
>> really found the tutorial section a great aid to my learning.
>
> You might like to wait a few weeks. The new edition of "Learning Perl
> Objects, References and Modules" - which has been renamed to "Intermediate
> Perl" will be released early in 2006.
Snappy title! Thanks for the pointer; I'll keep an eye on O'Reilly.
Justin.
--
Justin C, by the sea.
Re: push() to @array[$count] gives error, is there any other way?
am 31.12.2005 18:58:16 von NTUA
# This what you asked for
$ARGV[0] = 'abc abc , aa , bb cc dder3';
foreach(split/\s*,\s*/,$ARGV[0]){
($a,$_)=('array'.$i++,[split/\s+/]);*$a=$_;
$arraystrholder->{$a}=$_}
print $array0[0] , "\n";
print $array0[1] , "\n";
print $array1[0] , "\n";
print $array2[0] , "\n";
print $array2[1] , "\n";
print $array2[2] , "\n";
print $arraystrholder->{array0}[0] , "\n";
print $arraystrholder->{array0}[1] , "\n";
print $arraystrholder->{array1}[0] , "\n";
print $arraystrholder->{array2}[0] , "\n";
print $arraystrholder->{array2}[1] , "\n";
print $arraystrholder->{array2}[2] , "\n";