[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Syntax error question (newbie)



At 11:43 PM +0200 7/19/99, Giorgio Valoti wrote:
>Hi All!
>checking the sintax of this piece of script:
>
>for ($i = 0;
>     $i <= $#ID;
>     $i++) {
>  $prova =  @ID[$i];
>  print ("$prova\n");
>  foreach $x (0..$#ID) {
>    if ($ID[$x] !~ /[$prova]/) {
>      print ("$ID[$x]\n");
>
>    }
>  }
>}
>everything's OK.

Everything's OK? Not sure about that funny output from the second 
version, but, for starters, what output did you get from the first 
version?

Are you sure you want this phrase?

	$prova =  @ID[$i];

@ID[$i] gives a slice of the array @ID, in this case a one-element 
slice. By asking for its value in scalar context (by assigning it to 
$prova), what you get is the number of elements, (1). In almost all 
cases where one would use the C-ish structure
 
	for ($i = 0; $i <= $#ID; $i++) { ...

one really wants to process the scalar (string-like) contents of the 
i-th item of the array @ID, which is expressed $ID[$i] (note the 
$-sign before ID).

Change the @ to $ and see what happens. Also, give yourself the 
benefit of Perl warnings, by putting -w at the end of your shebang 
line

#!/usr/bin/perl -w     or whatever the ref to Perl is on your machine.

HTH

>but if I use:
>for ($i = 0;
>     $i <= $#ID;
>     $i++) {
>  $prova =  @ID[$i];
>  print ("$prova\n");
>  foreach $x (0..$#ID) {
>    if ($ID[$x] !~ /[$prova]/) {
>      print ("--- $ID[$x]\n");        #that's changed
>
>    }
>  }
>}
>
>I have:
># Can't open perl script "Voivod I:": No such file or directory.
>Am I missing something obvious?
>
>TIA
>
>Giorgio
>
>===== Want to unsubscribe from this list?
>===== Send mail with body "unsubscribe" to macperl-request@macperl.org

- Bruce

# ~~~~~~~~~~~~~~~~~~~
# Bruce Van Allen
# bva@cruzio.com
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Director
# WireService, an Internet service bureau
# Serving the educational and nonprofit sectors
# wire@wireservice.org
# http://wireservice.org
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org