Domanda

Questo non riesce:

my @a = ("a", "b", "c", "d", "e");
my %h = map { "prefix-

Questo non riesce:

Not enough arguments for map at foo.pl line 4, near "} @a"

con questo errore:

my @a = ("a", "b", "c", "d", "e");
my %h = map { "prefix-" . 

Questo non riesce:

my @a = ("a", "b", "c", "d", "e");
my %h = map { "prefix-

Questo non riesce:

Not enough arguments for map at foo.pl line 4, near "} @a"

con questo errore:

<*>

ma funziona:

<*>

perché?

" => 1 } @a;

con questo errore:

<*>

ma funziona:

<*>

perché?

=> 1 } @a;

ma funziona:

<*>

perché?

" => 1 } @a;

con questo errore:

<*>

ma funziona:

<*>

perché?

È stato utile?

Soluzione

Perché Perl sta indovinando un EXPR (un riferimento hash, ad esempio) invece di un BLOCCO. Questo dovrebbe funzionare (notare il simbolo '+'):

my @a = ("a", "b", "c", "d", "e");
my %h = map { +"prefix-

Perché Perl sta indovinando un EXPR (un riferimento hash, ad esempio) invece di un BLOCCO. Questo dovrebbe funzionare (notare il simbolo '+'):

<*>

Vedi http://perldoc.perl.org/functions/map.html .

" => 1 } @a;

Vedi http://perldoc.perl.org/functions/map.html .

Altri suggerimenti

Preferisco scriverlo come

my %h = map { ("prefix-

Preferisco scriverlo come

<*>

per mostrare l'intento, che sto restituendo un elenco di 2 elementi.

" => 1) } @a;

per mostrare l'intento, che sto restituendo un elenco di 2 elementi.

Da perldoc -f map :

           "{" starts both hash references and blocks, so "map { ..."
           could be either the start of map BLOCK LIST or map EXPR, LIST.
           Because perl doesn’t look ahead for the closing "}" it has to
           take a guess at which its dealing with based what it finds just
           after the "{". Usually it gets it right, but if it doesn’t it
           won’t realize something is wrong until it gets to the "}" and
           encounters the missing (or unexpected) comma. The syntax error
           will be reported close to the "}" but you’ll need to change
           something near the "{" such as using a unary "+" to give perl
           some help:

             %hash = map {  "\L

Da perldoc -f map :

<*>", 1 } @array # perl guesses EXPR. wrong %hash = map { +"\L

Da perldoc -f map :

<*>", 1 } @array # perl guesses BLOCK. right %hash = map { ("\L

Da perldoc -f map :

<*>", 1) } @array # this also works %hash = map { lc(

Da perldoc -f map :

<*>), 1 } @array # as does this. %hash = map +( lc(

Da perldoc -f map :

<*>), 1 ), @array # this is EXPR and works! %hash = map ( lc(

Da perldoc -f map :

<*>), 1 ), @array # evaluates to (1, @array) or to force an anon hash constructor use "+{" @hashes = map +{ lc(

Da perldoc -f map :

<*>), 1 }, @array # EXPR, so needs , at end and you get list of anonymous hashes each with only 1 entry.

Inoltre, l'altro modo di fare quello che stai facendo, inizializzando l'hash, puoi fare così:

my @a = qw( a b c d e );
my %h;
@h{@a} = ();

Ciò creerà voci indefinite per ciascuna delle cinque chiavi. Se vuoi dare loro tutti i veri valori, allora fallo.

@h{@a} = (1) x @a;

Puoi anche farlo esplicitamente con un ciclo;

@h{

Inoltre, l'altro modo di fare quello che stai facendo, inizializzando l'hash, puoi fare così:

my @a = qw( a b c d e );
my %h;
@h{@a} = ();

Ciò creerà voci indefinite per ciascuna delle cinque chiavi. Se vuoi dare loro tutti i veri valori, allora fallo.

@h{@a} = (1) x @a;

Puoi anche farlo esplicitamente con un ciclo;

<*>} = 1 for @a;

Penso che

map { ; "prefix-

Penso che

<*>

è più idiomatico, per quanto riguarda specificare che è un blocco di istruzioni e non un riferimento all'hash. Lo stai solo dando il via con una dichiarazione nulla.

" => 1 } @a;

è più idiomatico, per quanto riguarda specificare che è un blocco di istruzioni e non un riferimento all'hash. Lo stai solo dando il via con una dichiarazione nulla.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top