Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things
 
PerlMonks  

Re: scoping problems

by hdb (Parson)
on Jun 03, 2013 at 11:16 UTC ( #1036721=note: print w/ replies, xml ) Need Help??


in reply to scoping problems

I have not all answers but

  • In line 13, it should be if( and not (if which causes the syntax error.
  • Variables $saba and @saba are not related. This is the second error message. Do you mean $saba[$i] which refers to an element of @saba?
  • The error about $i requiring explicit package might be caused by the previous errors.




Comment on Re: scoping problems
Select or Download Code
Re^2: scoping problems
by naturalsciences (Beadle) on Jun 03, 2013 at 11:19 UTC
    Sorry 'bout a ninja updater I spotted and replaced if ($saba[$i]!~m/unclassified/) myself. (there was $_ instead of $saba$i ) Thanks for the other bugs - I look at these and we'll see if the scoping problem persist and what do to with it in this case. For $saba I tried to use @saba in scalar context to get nr. of elements - probably shoudl write it out somewhere - like $newscalar=$saba etc?
Re^2: scoping problems
by naturalsciences (Beadle) on Jun 03, 2013 at 11:30 UTC

    OK. Thanks for pointing out the $saba and @saba not being related. It is probably bug out of convenience - I seem to have used an array in scalar context easily getting an array length but it seems that a shorthand like this cant travel well around different loops etc.? I did put

    for(my $i=0;$i<@saba;++$i) {my $sabasize=scalar(@saba);

    on the top of the for loop and it seems to be almost getting somewhere from there. :D so it seems to be that you led me to a better path here.

      If you use @saba, not $saba, in scalar context, you get the arraysize indeed. This is why your $i<@saba works.

      Further to hdb's reply:

      ... it seems that a shorthand like [evaluating an array in scalar context] cant travel well around different loops etc.?

      It travels perfectly well. hdb and others have already pointed out that  @saba and  $saba are quite different things and are in no way inherently related.

      What may be confusing you is the fact that an element of the  @array array is accessed by the syntax  $array[n] (note the  $ sigil). The logic of this, determined by Larry at the Dawn of Time (Perl version 1.0), is that the elements of arrays (and also of hashes – associative arrays) are always and only scalars. But you always have to distinguish between an array, which contains (like a hash) a certain number of elements and thus has, among other properties, a size, and an element of an array.

        The logic of this, determined by Larry at the Dawn of Time (Perl version 1.0), is that the elements of arrays (and also of hashes associative arrays) are always and only scalars.

        Interestingly, this logic (which sounds perfectly sane to me) has been abandoned in Perl 6. I don't understand why.

        -- FloydATC

        Time flies when you don't know what you're doing

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-04-18 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls