newbie learning perl 
Author Message
 newbie learning perl

I'm reading "learning Perl" and have tried some of the exercises and have
had some sucsess runnitn them on Win(NT). But I've had some problems getting
them to work on a linux OS.

The following hash:

%words =qw(
        me    my
        I        it
        one    two
        be      been
);

Print "$words{me}"

returns the following error message:

"Can't modify not in scalar assignment at hash line (number), near ");"

As mentioned, this works on Win(NT) but not RH Linux. I know there are major
differences between the two versions. But I expected the basics to work. Can
anyone give me some direction with this.

Thanks in advance.



Sun, 16 May 2004 19:50:05 GMT  
 newbie learning perl

I limited my response to clpm because this is not an OS issue.

Quote:

>I'm reading "learning Perl" and have tried some of the exercises and have
>had some sucsess runnitn them on Win(NT). But I've had some problems getting
>them to work on a linux OS.

>The following hash:

>%words =qw(
>        me    my
>        I        it
>        one    two
>        be      been
>);

>Print "$words{me}"

print, not Print.

Quote:

>returns the following error message:

>"Can't modify not in scalar assignment at hash line (number), near ");"

When I run the code you posted, I get this message:

String found where operator expected at ./junk.pl line 9, near "Print
        "$words{me}"" (Do you need to predeclare Print?)  syntax error
        at ./junk.pl line 9, near "Print "$words{me}"" Execution of
        ./junk.pl aborted due to compilation errors.

which makes sense to me.  The error message you've provided doesn't
make sense to me in the context of your example.  Did you provide the
entire example?

Quote:

>As mentioned, this works on Win(NT) but not RH Linux. I know there
>are major differences between the two versions.

I would strongly disagree with the assertion that there are 'major
differences between the two versions'.  One of the reasons I use Perl
is because I can write programs which run without modification on NT,
linux, solaris, hpux and aix.

Quote:
>But I expected the basics to work. Can anyone give me some direction
>with this.

Correct the 'print' statement, and if that doesn't work, cut and paste
(please don't type) the entire example.

--
-- Steve __



Sun, 16 May 2004 20:11:48 GMT  
 newbie learning perl
1. The error message given was quoted.
2. The difference between the two versions I believe is in functionality.
Win(NT) doen't have a compiler, whereas RH linux is supposed tobe so
endowed.
3. How do I declare a print statement. I'm still in the first chapter of
"Learning Perl. I'm trying to make sure this program is working on Linux.
4. Thanks for responding.

Quote:

> I limited my response to clpm because this is not an OS issue.


> >I'm reading "learning Perl" and have tried some of the exercises and have
> >had some sucsess runnitn them on Win(NT). But I've had some problems
getting
> >them to work on a linux OS.

> >The following hash:

> >%words =qw(
> >        me    my
> >        I        it
> >        one    two
> >        be      been
> >);

> >Print "$words{me}"

> print, not Print.

> >returns the following error message:

> >"Can't modify not in scalar assignment at hash line (number), near ");"

> When I run the code you posted, I get this message:

> String found where operator expected at ./junk.pl line 9, near "Print
> "$words{me}"" (Do you need to predeclare Print?)  syntax error
> at ./junk.pl line 9, near "Print "$words{me}"" Execution of
> ./junk.pl aborted due to compilation errors.

> which makes sense to me.  The error message you've provided doesn't
> make sense to me in the context of your example.  Did you provide the
> entire example?

> >As mentioned, this works on Win(NT) but not RH Linux. I know there
> >are major differences between the two versions.

> I would strongly disagree with the assertion that there are 'major
> differences between the two versions'.  One of the reasons I use Perl
> is because I can write programs which run without modification on NT,
> linux, solaris, hpux and aix.

> >But I expected the basics to work. Can anyone give me some direction
> >with this.

> Correct the 'print' statement, and if that doesn't work, cut and paste
> (please don't type) the entire example.

> --
> -- Steve __



Sun, 16 May 2004 20:19:46 GMT  
 newbie learning perl

<snippage>

Quote:
> Print "$words{me}"

If I change this to

  print $words{'me'};

it works on my Linux system.

Quote:
> returns the following error message:

> "Can't modify not in scalar assignment at hash line (number), near ");"

Huh?

Quote:
> As mentioned, this works on Win(NT) but not RH Linux. I know there are
> major differences between the two versions. But I expected the basics to
> work. Can anyone give me some direction with this.

Not that much of a difference... Are you sure that's the error you got???

--
Ciaran McCreesh

www:      http://www.opensourcepan.co.uk/



Sun, 16 May 2004 20:59:59 GMT  
 newbie learning perl

Quote:

> 2. The difference between the two versions I believe is in functionality.
> Win(NT) doen't have a compiler, whereas RH linux is supposed tobe so
> endowed.

There are no real differences between them. That's the point of a
general programming language that is multi-platform.

Quote:
> 3. How do I declare a print statement. I'm still in the first chapter of
> "Learning Perl. I'm trying to make sure this program is working on Linux.

You don't, you just type it correctly, ie. "print" with a lower case
'p'.

This works just fine under Solaris (I added a newline):


%words =qw(
        me    my
        I        it
        one    two
        be      been
);

print "$words{me}\n"

my

with Perl 5.6.1. Try it again and when you copy something to test it,
pay attention to case.

cdh

--
Chris D. Halverson                              http://www.halverson.org/



Sun, 16 May 2004 21:12:48 GMT  
 newbie learning perl

Quote:

> 3. How do I declare a print statement.

He was telling you that Perl interpretted your "Print" as something other than
the directive "print".  Did you change Print to print and re-attempt your exercise?

-Bob



Sun, 16 May 2004 21:16:24 GMT  
 newbie learning perl
I think in this case, since a similar hash structure works fine under
Linux, is that your post, as an attachment, the snippet of Perl code
(containing your hash) that yields in your posted error.  Remember, a
single keystroke can make all the difference in the world.  For
example, you have "Print" instead of "print", which will fail in Perl
as a syntax error.  Also, if your code snippet is not in full context,
errors can originate in one place, and manifest themselves somewhere
else.

Chris



Quote:
>I'm reading "learning Perl" and have tried some of the exercises and have
>had some sucsess runnitn them on Win(NT). But I've had some problems getting
>them to work on a linux OS.

>The following hash:

>%words =qw(
>        me    my
>        I        it
>        one    two
>        be      been
>);

>Print "$words{me}"

>returns the following error message:

>"Can't modify not in scalar assignment at hash line (number), near ");"

>As mentioned, this works on Win(NT) but not RH Linux. I know there are major
>differences between the two versions. But I expected the basics to work. Can
>anyone give me some direction with this.

>Thanks in advance.



Sun, 16 May 2004 21:54:48 GMT  
 newbie learning perl
On Wed, 28 Nov 2001 19:19:46 GMT, Harvey said:

Quote:
>2. The difference between the two versions I believe is in functionality.
>Win(NT) doen't have a compiler, whereas RH linux is supposed tobe so
>endowed.

This is not correct. The perl executable compiles your script to a
bytecode on both platforms, then runs that bytecode through the
interpreter. Perhaps you are thinking of C -- on unixlike systems
you usually get a C compiler included, while with Windows, that's
an extra.

Quote:
>3. How do I declare a print statement. I'm still in the first chapter of
>"Learning Perl. I'm trying to make sure this program is working on Linux.

Others have pointed out you need to correct the typo for print().
Further, you probably need to make sure that your line endings are
converted appropriately when transferring between platforms.

Since you are new to Perl, it is worth pointing out that you should
*always* enable warnings and 'use strict' to take advantage of the
error checking features built in to Perl. You would do this by
starting your programs with something like:

   #!/path/to/perl -w
   use strict;

Note that this will require you to declare your variables' scope.
For lexical variables (which you will want to use exclusively at
this stage), you will need to say something like:

   my $varname = 'whatever';

The first time each variable is used. This might seem like a PITA
at first, but actually saves you a LOT of time when you have bugs.

If you find the reporting from strict and arnings too cryptic,
including:

   use diagnostics;

as well uses a lookup table to expand the error messages into
a more verbose and (perhaps) meaningful form.

You should also familiarise yourself with the online documentation
included with perl: try typing 'perldoc perldoc' at the command
prompt.

Hope this helps and good luck,

Cheers,
Damian



Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub



Sun, 16 May 2004 23:36:24 GMT  
 newbie learning perl

Quote:

> I'm reading "learning Perl" and have tried some of the exercises and have
> had some sucsess runnitn them on Win(NT). But I've had some problems getting
> them to work on a linux OS.

> The following hash:

> %words =qw(
>         me    my
>         I        it
>         one    two
>         be      been
> );

> Print "$words{me}"

> returns the following error message:

> "Can't modify not in scalar assignment at hash line (number), near ");"

> As mentioned, this works on Win(NT) but not RH Linux. I know there are major
> differences between the two versions. But I expected the basics to work. Can
> anyone give me some direction with this.

> Thanks in advance.

change your print line to (pay attention to case and punctuation):

print $words{me} ;
^                ^

small p in print, end with semicolon. Your scripts will work on Linux no
problem, if you add  #!/usr/bin/perl -w (for RedHat Linux)
and change
print $words{me};  to: print "$words{me}\n";



Mon, 17 May 2004 02:19:44 GMT  
 newbie learning perl
Only necessary if the key name is more than one word:

        $words{'me too'} = boo;
        print "$words{'me too'}\n";

Chris

On Thu, 29 Nov 2001 13:06:03 +1100, "Skeleton Man"

Quote:

>Shouldn't there be single quotes around the key name ?
>e.g. print "$words{'me'}\n";
>                            ^   ^



Mon, 17 May 2004 04:30:17 GMT  
 newbie learning perl
[posted & mailed]

On Nov 28, Harvey said:

Quote:
>%words =qw(
>        me    my
>        I        it
>        one    two
>        be      been
>);

>Print "$words{me}"

Assuming you meant 'print'...

Quote:
>returns the following error message:

>"Can't modify not in scalar assignment at hash line (number), near ");"

Heh, you've left out the line that is causing that error, but the error
message clinches it for me.

You have written:

  !#/usr/bin/perl

  %hash = (...);

which parses as

  ! %hash = (...);

since #... denotes a comment.  You have transposed the ! and # characters,
so your code should be:

  #!/usr/bin/perl

  %hash = (...);

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **



Mon, 17 May 2004 07:43:30 GMT  
 newbie learning perl
Have discovered the problem had nothing to do with OS or the way I
originally wrote the script.  The problem turned out to be a transposed
shebang. I began the file with !# and corrected it with #!. All my scripts
work better. Thanks for the help and sorry for consuming your time on
foolishness.

Quote:
> Only necessary if the key name is more than one word:

> $words{'me too'} = boo;
> print "$words{'me too'}\n";

> Chris

> On Thu, 29 Nov 2001 13:06:03 +1100, "Skeleton Man"

> >Shouldn't there be single quotes around the key name ?
> >e.g. print "$words{'me'}\n";
> >                            ^   ^



Thu, 20 May 2004 20:27:43 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Newbie learning Perl

2. newbie learning perl

3. newbie:Learning Perl

4. help - newbie learning perl

5. Learning Perl(Unix) by O'Reilly in exchange for Learning Perl on Win32 Systems

6. Newbie - Should I learn Perl or Javascript

7. Script on input to perl arrays wanted - Newbie learning

8. Newbie "Learning Perl" help

9. newbie question from Learning Perl--help me debug this

10. Learning Object Oriented Programming (newbie)

11. newbie: need help,LEARNING FROM A BOOK

12. newbie learning "my" declarations

 

 
Powered by phpBB® Forum Software