I guess that you have code that works to your satisfaction. But I thought I'd point this out if you haven't realized it yet.... A Perl 2D structure is very similar to a dynamically built 2D structure in C (not what the compiler does for a static 2D declaration).

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.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @deck = deck_maker(); print Dumper \@deck; sub deck_maker { my @deck = (); #the deck for returning my @types = ('spades', 'hearts', 'diamonds', 'clubs'); my @cards = ( [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'] ); for ( my $i=0; $i < 4; $i++) { for ( my $j=0; $j < 13; $j++) { my $newcard = "$cards[$i][$j] of $types[$i]";##### push(@deck, $newcard); } } return @deck; #return the new deck }
As previously mentioned, I wouldn't write the code this way, but for example, this is possible:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @deck = deck_maker(); print Dumper \@deck; sub deck_maker { my @deck = (); #the deck for returning my @types = ('spades', 'hearts', 'diamonds', 'clubs'); for ( my $i=0; $i < 4; $i++) { my @cards = ( 2,3,4,5,6,7,8,9,10,'J','Q','K','A'); while (my $card = pop @cards) { my $newcard = "$card of $types[$i]"; push(@deck, $newcard); } } return @deck; #return the new deck }
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.

In reply to Re^4: What I am missing here by Marshall
in thread What I am missing here by heatblazer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.