Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Syntax error with loops

by OxYgEn (Acolyte)
on Feb 25, 2004 at 03:03 UTC ( #331582=perlquestion: print w/replies, xml ) Need Help??
OxYgEn has asked for the wisdom of the Perl Monks concerning the following question:

I'm brand new at perl. Basically all I'm trying to do is make a thing that lists each variable in an array. My logic is that after each time it prints the name of each variable, it increases a different variable which controls which variable it's going to print next. OK that was total jibberish I hope you understood that. Here's an example:
@HappyArray=("Perl","Is","Confusing","Would","Someone","Please","Help" +,"Me"); $printnumber=-1; foreach (@HappyArray) { inc $printnumber 1; print "$HappyArray[$printnumber]\n"; } die "Done!\n";

When I run the script, I get this error:
Can't call method "inc" without a package or object reference at line 5.

Isn't $printnumber the object reference??

20040225 Edit by BazB: Changed title from 'NEWB NEWB NEWB'

Replies are listed 'Best First'.
Re: Syntax error with loops
by Roger (Parson) on Feb 25, 2004 at 03:37 UTC
    You immediate problem has been resolved. However I would like to point out that the perlish way is lot simpler than that. I have rewritten your example as follows:
    use strict; use warnings;
    It's a good practise to add the above lines to every script you write. It will save you a lot of time and efford in the future.

    my @HappyArray = qw/ Perl Is Confusing Would Someone Please Help Me/;
    Use "qw" (quoted words) to build your list and save some typing.

    foreach (@HappyArray) { print "$_\n"; }
    The foreach/for function iterates through the elements in the list. If you do not specify an interation variable, it will localize the special perl variable "$_" and set it to the value of the element in each iteration. It can even be written as:
    print "$_\n" for @HappyArray;

    So the complete program looks like this:
    use strict; use warnings; my @HappyArray = qw/ Perl Is Confusing Would Someone Please Help Me/; print "$_\n" for @HappyArray; die "Done!";

Re: Syntax error with loops
by pzbagel (Chaplain) on Feb 25, 2004 at 03:36 UTC

    Welcome to Perl. If you stick to this long enough you will find that "There is more than one way to do it" in Perl. Also, there has been a lot of work put in to make Perl simple like shell scripting yet powerful like C. For example, you can simplify your code above in many ways. Changing the Output Record Seperator, using join(). But since you are starting out, you can begin with the $_ variable.

    The $_ or default variable is used by many functions in Perl, including the for or foreach functions. You see for will automatically assign the values within @HappyArray to the $_ variable one-at-a-time so you can access them within the for loop:

    for (@HappyArray) { print "$_\n"; }

    BTW, for and foreach are synonymous as far as Perl is concerned.

    No counters, no iteration, just a simple loop which is much easier to understand.

    Hope that helps. Keep learning.

Re: Syntax error with loops
by atcroft (Abbot) on Feb 25, 2004 at 03:16 UTC

    @HappyArray is an array of strings, and $printnumber is the index of the element you are referring to. Change line 5 of your snippet to either: $printnumber++; or $printnumber += 1; and you will be okay. (Perl doesn't have an inc() function that I'm aware of, or that I can find in the Library section here.)

    Hope that helps.

Re: Syntax error with loops
by adamk (Chaplain) on Feb 25, 2004 at 06:00 UTC
    Let me also just add that the following command is completely invalid
    inc $printnumber 1;
    'inc' is just not perl command...

    Perl CAN, of course, increment a number, using $printnumber++ much like in C.

    What actually happened is that perl often interprates 'foo bar' as 'bar->foo', a method call. This lets things like the following work naturally
    # Java'ish looking way my $Object = new Some::Class( $arg ); # Simple method call my $Object = Some::Class->new( $arg );
Re: Syntax error with loops
by bart (Canon) on Feb 25, 2004 at 18:32 UTC
    Isn't $printnumber the object reference??
    Well... your problem seems to be that in Perl, not everything is an object. A number is not an object.

    So, you have to use ordinary operators for ordinary variables to make it do what you want. Here that would be something like

    $printnumber += 1;

    Just for fun, I'll make $printnumber an object, and have it accept your syntax:

    #!/usr/bin/perl -w { package NumberObject; use overload '""' => \&as_string, '0+' => \&as_number, '+' => \&ad +d; sub new { my $class = shift; my $value = shift; bless \$value, $class; } sub as_string { return "" . ${+shift}; } sub as_number { return 0 + ${+shift}; } sub add { my $self = shift; return ref($self)->new($self->as_number + shift); } sub inc { my $self = shift; $$self += shift; } } $\ = "\n"; my $printnumber = new NumberObject(-1); print "initial value:"; print $printnumber; print "looping:"; for (1 .. 10) { inc $printnumber 1; print $printnumber; } print "overload magic (adding 5):"; $printnumber += 5; print $printnumber; print "still an object (adding 2):"; inc $printnumber 2; print $printnumber;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://331582]
Approved by arden
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-20 04:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.