in reply to What I am missing here
Not sure if this has been mentioned yet, but the warning in your screenshot comes from:
my $newcard = pop(@cards[$i]) . " of " . $types[$i];
I'm not sure that is supposed to work (no idea how pop handles array slices), but you meant pop(@{$cards[$i]})
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What I am missing here
by aaron_baugher (Curate) on Mar 21, 2012 at 11:49 UTC | |
Interestingly, if I pass pop a slice, it seems to operate on the last element in the slice, popping the last element from the array to which that element is a reference. If I pass it a slice of ordinary scalars (not array references) it errors. All this is since version 5.14, by the way; prior to that pop simply demands an array as the first argument. Also, perldoc -f pop says this is experimental behavior, so it'd probably be best to keep dereferencing for now and not count on this.
Aaron B. | [reply] [d/l] [select] |
by heatblazer (Scribe) on Mar 21, 2012 at 17:14 UTC | |
My only vision of the things here ( and I am a Perl noob) is that pop @ar0,1 is possible by poping index1 and index0 sequentially, the problem with pop @ar0..3 is that you try to pop a list probably, that`s my assumption tho, it may be completely wrong. The problem with pop is more verbal than system, it`s hard for us humans to read than the compiler to turn to 0s and 1s, when I say:
I actually want a scalar variable, but I have to know that $storage[0] is a index to an array from which I`ll pop something. It`s tricky and definitely not a noob-friendly, so noobs like me should read Perl books 2 times more cautiously. | [reply] [d/l] |
by Marshall (Canon) on Mar 23, 2012 at 00:16 UTC | |
In C I would malloc an array of pointers (the number of rows), then for each row, malloc an array for the data and set the row pointer for that row to point to that array of data. Perl works the same way. @cards is an array of pointers to array. You can use the $card[$i][$j] syntax just like in C, card[i][j]. There is no need here to be "popping" anything. You have a mix of Perl ideas with C ideas and this combination creates some confusing code! As you write more Perl code, you will find that C style "for" loops are rare. The dreaded "off by one error", which statistically is there most common programming error by many studies, is much less likely in Perl because under most circumstances you don't have to explicitly specify array index bounds like in C. This is a major advantage to Perl. There is other "magic" in Perl, for example, the core module Data::Dumper which can dump any data structure of arbitrary complexity - there is no equivalent in C. As previously mentioned, I wouldn't write the code this way, but for example, this is possible: Every time Perl sees "my", you get a "new" one. Perl will re-use the same memory if it can. The syntax that you wound up with generates a run-time error. Why get more complicated than necessary? No need to use a multidimensional structure when a 1D structure will work. Note that in my "while" loop, I don't have to know how many cards are there. | [reply] [d/l] [select] |
|
Re^2: What I am missing here
by heatblazer (Scribe) on Mar 21, 2012 at 11:07 UTC | |
That`s some trick I am not quite good at... Yes the point is good but I want to pop an element from an array it can be written like @cards->$i but there is still a warning ( not an error which can be omitted for now ). I don`t pop a slice actually, I just pop last element from N row of the 2D array and that was my solution, for good or bad Doing it the way:
solves the warning but I am not sure why since I want to pop an array variable not a scalar one... Guess I need more Perls to eat :) | [reply] [d/l] |