[FR/EN] faire un tableau / to make a table

A

Alextophi

FR ----------------------------------------------
Bonjour

Comment faire un tableau avec 1 'clé' avec 2 valeurs.

ex: {clé} = (valueA, valueB)


EN ----------------------------------------------
Hello

How to make a table with 1 'key' with 2 values.

ex : {key} = (valueA, valueB)


regard

ch.
 
J

Josef Moellers

Alextophi said:
FR ----------------------------------------------
Bonjour

Comment faire un tableau avec 1 'clé' avec 2 valeurs.

ex: {clé} = (valueA, valueB)


EN ----------------------------------------------
Hello

How to make a table with 1 'key' with 2 values.

ex : {key} = (valueA, valueB)

use warnings;
use strict;
my %table = ();
$table{key} = [ 'valueA', 'valueB' ];
print $table{key}->[0], "\n";
print $table{key}->[1], "\n";

valueA
valueB
 
J

Josef Moellers

George said:
Alextophi wrote:




@{$key}=("value1","value2")

use warnings; use strict;
@{$key}=("value1","value2")

Global symbol "$key" requires explicit package name at - line 2.
Execution of - aborted due to compilation errors.
 
G

George

Alextophi said:
FR ----------------------------------------------
Bonjour

Comment faire un tableau avec 1 'clé' avec 2 valeurs.

ex: {clé} = (valueA, valueB)


EN ----------------------------------------------
Hello

How to make a table with 1 'key' with 2 values.

ex : {key} = (valueA, valueB)


regard

ch.

@{$key}=("value1","value2")
 
G

George

Josef said:
use warnings; use strict;
@{$key}=("value1","value2")

Global symbol "$key" requires explicit package name at - line 2.
Execution of - aborted due to compilation errors.

should I also tell him to install perl,dumb head
 
P

Paul Lalli

George said:
@{$key}=("value1","value2")

This creates an array reference named $key with two values. The fact
that the OP is using the terms 'key' and 'values' suggests he's looking
for a hash, not an array, where the keys of the hash can have more than
one value in the hash. Your example simply creates one array
reference, with no association to any other 'keys'.

my %hash;
$hash{'key'} = [qw/value1 value2/];
$hash{'key2'} = [qw/value3 value4/];
#etc ...

Paul Lalli
 
A

A. Sinan Unur

should I also tell him to install perl,dumb head

No, but you should realize you are in no position to hurl insults at
people, let alone give advice.

I presume you meant to use a hash slice above. But even with a hash
slice, you get the following:

#!/usr/bin/perl

use strict;
use warnings;

my %hash;
@hash{key} = ('value1', 'value2');

use Data::Dumper;
print Dumper \%hash;

__END__

D:\Home\asu1\UseNet\clpmisc> z
Scalar value @hash{key} better written as $hash{key} at D:\Home\asu1
\UseNet\clpmisc\z.pl line 7.
$VAR1 = {
'key' => 'value1'
};

To have a hash element refer to more than one element, you need to set
$hash{key} to an array reference:

$hash{key} = ['value1', 'value2'];

Oh, by the way *PLONK*

Sinan
 
J

Josef Moellers

George said:
Josef Moellers wrote:




should I also tell him to install perl,dumb head

I always put my name on a separate line, but then ...

Your solution does not pass the "use strict", which is sort of a
prerequisite for proper solutions.

Stay cool,

Josef
 
G

George

Josef said:
I always put my name on a separate line, but then ...

Your solution does not pass the "use strict", which is sort of a
prerequisite for proper solutions.

Stay cool,

Josef

I never wanted to give the complete code, and why should I , I answered
what I though OP asked, Paul Lalli pointed that out that mistake,
people many sometime just point towards documentation should OP cut and
paste those lines in perl code and compile them as it is?
 
G

George

A. Sinan Unur said:
should I also tell him to install perl,dumb head

No, but you should realize you are in no position to hurl insults at
people, let alone give advice.

I presume you meant to use a hash slice above. But even with a hash
slice, you get the following:

#!/usr/bin/perl

use strict;
use warnings;

my %hash;
@hash{key} = ('value1', 'value2');

use Data::Dumper;
print Dumper \%hash;

__END__

D:\Home\asu1\UseNet\clpmisc> z
Scalar value @hash{key} better written as $hash{key} at D:\Home\asu1
\UseNet\clpmisc\z.pl line 7.
$VAR1 = {
'key' => 'value1'
};

To have a hash element refer to more than one element, you need to
set $hash{key} to an array reference:

$hash{key} = ['value1', 'value2'];

Oh, by the way PLONK

Sinan

what are you trying to say by these lines
 
J

John Bokma

George said:
what are you trying to say by these lines

That he has configured his news reader to hide all messages written by you
in this group ( and maybe others as well) from now on. (Hence I answer,
because he isn't going to ;-) )

I am sure there was a good reason, which might mean others have done the
same. It also might mean that next time you post a question here you get
not as much replies as expected.
 
J

Josef Moellers

George said:
Josef Moellers wrote:




I never wanted to give the complete code, and why should I

You don't have to post "complete code", at least, I didn't.
However, you posted a bad example as it manipulates the symbol table.
The reference to @{...} is considered bad code and "use strict" is there
to catch these. Uncounted replies have pointed out that using a hash is
The Right Thing to do.
, I answered
what I though OP asked, Paul Lalli pointed that out that mistake,
people many sometime just point towards documentation should OP cut and
paste those lines in perl code and compile them as it is?

Sorry, but I can't quite parse this.

If a question is answered by one of the articles in the FAQ or
documentation, then pointing people at the FAQ list is OK. IMHO, if
answering the question requires less keystrokes than pointing at the
FAQ, then I do prefer that.
When I post a code snipped, I try to adhere to posting rules in that the
code posted should pass
use warnings;
use strict;
and, if possible, it should have been tested (putting them within
<untested>, </untested> might be OK in some circumstances).
If I correct someone's code snippets, then, yes, the OP should be able
to cut-and-paste the correction. In this case, as the question was about
a technique, rather than a non-working code (Alextophi didn't post any
code that is proper Perl), no cut-and-paste solution could be given.

But I stand by my statement that the code line you provided does not
pass "use strict" and, as such, does not adhere to posting rules here.

Your attitude ("dumb head", "why should I") strikes me as being
impolite, but that is my very personal feeling and others might regard
you as being nice and considerate (as it appears, some do not).

This now has little to do with Perl any more, so I think we should close
this debate here.

Josef
 
G

George

Josef said:
You don't have to post "complete code", at least, I didn't.
However, you posted a bad example as it manipulates the symbol table.
The reference to @{...} is considered bad code and "use strict" is
there to catch these. Uncounted replies have pointed out that using a
hash is The Right Thing to do.


Sorry, but I can't quite parse this.

If a question is answered by one of the articles in the FAQ or
documentation, then pointing people at the FAQ list is OK. IMHO, if
answering the question requires less keystrokes than pointing at the
FAQ, then I do prefer that.
When I post a code snipped, I try to adhere to posting rules in that
the code posted should pass
use warnings;
use strict;
and, if possible, it should have been tested (putting them within
<untested>, </untested> might be OK in some circumstances).
If I correct someone's code snippets, then, yes, the OP should be
able to cut-and-paste the correction. In this case, as the question
was about a technique, rather than a non-working code (Alextophi
didn't post any code that is proper Perl), no cut-and-paste solution
could be given.

But I stand by my statement that the code line you provided does not
pass "use strict" and, as such, does not adhere to posting rules here.

Your attitude ("dumb head", "why should I") strikes me as being
impolite, but that is my very personal feeling and others might
regard you as being nice and considerate (as it appears, some do not).

This now has little to do with Perl any more, so I think we should
close this debate here.

Josef

I got the point, and so no hard feeling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top