Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Array storage issue

by hippo (Deacon)
on Apr 06, 2014 at 21:44 UTC ( #1081339=note: print w/ replies, xml ) Need Help??


in reply to Array storage issue

One obvious question: why do you have this pathological aversion to lexicals?


Comment on Re: Array storage issue
Re^2: Array storage issue
by LanX (Canon) on Apr 06, 2014 at 21:52 UTC
    Even if he doesn't like lexicals, using package and our would facilitate reading his code.

    I suppose he saw strict complaining about undeclared variables and took it literally. :(

    like:

    > perl use strict; $x=23; __END__ Global symbol "$x" requires explicit package name at - line 2.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      My issue isn't strict, I don't know why everyone says that. I have a small sample program that works perfectly, but I'm trying to merge something like it into the main code I'm working on. I think my problem is in the hd::store=[]; statement. When I push things into another array, does it push the values or just a reference to the values? Can I make it push just the values?

        Perl arrays store scalar values. Scalar values are things like numbers, strings and references. When you copy an array (which includes push, pop and so on) you copy the scalar values of the elements. In a sense Perl doesn't do multiply dimensioned arrays. Instead it allows you to have an array of references to other (lets call them 'owned') arrays. When you copy an array of arrays you only copy the references so you end up with two arrays containing references to the same collection 'owned' arrays.

        You need to provide a way to make a deep copy of the data. Data::Deep may help (I've not used it). It may also be worth looking at PDL which is designed for data manipulation in Perl.

        BTW, I strongly endorse the other comments relating to using 'my' and package variables. Most likely if stuff broke when you used 'my' strict was highlighting either a bug or a design problem in your code.

        Perl is the programming world's equivalent of English
Re^2: Array storage issue
by viored (Novice) on Apr 06, 2014 at 22:01 UTC
    Sorry, I don't know what that is. All the perl I know I've self-taught from online function guides. I write things this way because it works, I'm not great at it.

      The unwisdom of your beguilement with package globals may be demonstrated by the following code. Can you explain the difference between the outputs of the two loops?

      c:\@Work\Perl\monks>perl -wMstrict -e "for ($loop::i = 0; $loop::i < 3; $loop::i++) { clust($loop::i) } print qq{\n}; ;; for (my $i = 0; $i < 3; $i++) { clust2($i); } ;; ;; sub clust { my ($n) = @_; ;; for ($loop::i = 0; $loop::i < 3; $loop::i++) { my $m = $n * $loop::i; print qq{= $m }; } print qq{\n}; } ;; sub clust2 { my ($n) = @_; ;; for (my $i = 0; $i < 3; $i++) { my $m = $n * $i; print qq{- $m }; } print qq{\n}; } " = 0 = 0 = 0 - 0 - 0 - 0 - 0 - 1 - 2 - 0 - 2 - 4

      Global data is, IMHO, always problematic. If you are looking for up-to-date on-line general introductory tutorials, I would suggest perlintro, perhaps followed by chromatic's freely downloadable Modern Perl.

      Sorry, I don't know what that is. All the perl I know I've self-taught from online function guides. I write things this way because it works, I'm not great at it.

      When you get the time :) Tutorials has a bunch

      Modern Perl by chromatic a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.

      Learn Perl in about 2 hours 30 minutes a tutorial reviewed and recommended by Perl Tutorial Hub

      All of these, like perlintro, teach about Coping with Scoping , using lexical variables, because when programs grow beyond one screen , you'll want and argument passing

Re^2: Array storage issue
by viored (Novice) on Apr 06, 2014 at 22:17 UTC
    Oh, if you mean why do I define all my variables with packages like main? Because when I first started perl, whenever I tried to use "my" my program wouldn't work. So I just started giving things packages, and haven't stopped. It just seems more stable.
      > It just seems more stable.

      no they are definitely not.

      Package variables should be the well chosen exception, because they are global.

      And sorry your code is unreadable for me... so I can't help.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      ... when I first started perl, whenever I tried to use "my" my program wouldn't work.

      It would have been very useful to have explored the reasons why your use of lexical variables wouldn't work. It's not too late to begin!

      ... packages ... just [seem] more stable.

      Package data is global data. Global data is, IMHO, always problematic.

      The story goes that during one of the Pearl Harbor attacks on December 7, 1941, General Walter Short, commander of the Army forces in Pearl, was struck in the chest by a spent 50-caliber machinegun round and knocked to the ground. The bullet's impact didn't even break the skin, but when someone picked it up and showed it to Short, he said "It would have been better if it had killed me."

      When the day comes (and come it will if it has not already) that you are knocked on your ass by a chunk of global data at the end of a long trajectory from its point of origin, you may have some of the same feelings.

        I somehow expected a metaphor about Japanese bullets being global, while American generals wear lexical uniforms...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-07-26 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (177 votes), past polls