newbie learning perl

newbie learning perl

Post by Harve » Fri, 30 Nov 2001 03:50:05



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.

 
 
 

newbie learning perl

Post by Ciaran McCrees » Fri, 30 Nov 2001 04:59:59


<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/

 
 
 

newbie learning perl

Post by Kosh Vade » Fri, 30 Nov 2001 05:54:48


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.

 
 
 

newbie learning perl

Post by HoboSo » Fri, 30 Nov 2001 10:19:44



> 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";

 
 
 

newbie learning perl

Post by Kosh Vade » Fri, 30 Nov 2001 12:30:17


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";
>                            ^   ^

 
 
 

newbie learning perl

Post by Harve » Tue, 04 Dec 2001 04:27:43


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.

> 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";
> >                            ^   ^