|
|
| The Ghost 2006-01-20, 6:59 pm |
| I have keys with periods in them:
my %hash;
$hash{something.withaperiod}="some text";
my $something='something';
my $withaperiod='withaperiod';
print qq{$hash{"$something.$withaperiod"}\n};
what will it print?
| |
| Chas Owens 2006-01-20, 6:59 pm |
| On 1/20/06, The Ghost <ghost@madisonip.com> wrote:
> I have keys with periods in them:
>
> my %hash;
> $hash{something.withaperiod}=3D"some text";
> my $something=3D'something';
> my $withaperiod=3D'withaperiod';
> print qq{$hash{"$something.$withaperiod"}\n};
>
>
> what will it print?
It will print nothing; something.withaperiod is not a bareword. Try
putting it in quotes
$hash{'something.withaperiod'}=3D"some text";
| |
| Chas Owens 2006-01-20, 6:59 pm |
| snip
> It will print nothing; something.withaperiod is not a bareword. Try
> putting it in quotes
>
> $hash{'something.withaperiod'}=3D"some text";
>
Or, more accurately, 'something' and 'withaperiod' are barewords that
are getting joined by the '.'.
You should really start using the 'use warnings;' pragma. It and the
'use strict;' pragma will save you a lot of time.
| |
| Brenden Eng 2006-01-20, 6:59 pm |
| That will produce a bunch of errors.
You need to quote the key containing a period in it.
Example:
$hash{"something.withaperiod"}="some text";
This produces the expected output.
On 1/20/06, The Ghost <ghost@madisonip.com> wrote:
>
> I have keys with periods in them:
>
> my %hash;
> $hash{something.withaperiod}="some text";
> my $something='something';
> my $withaperiod='withaperiod';
> print qq{$hash{"$something.$withaperiod"}\n};
>
>
> what will it print?
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Paul Lalli 2006-01-20, 6:59 pm |
| Brenden Eng wrote:
> On 1/20/06, The Ghost <ghost@madisonip.com> wrote:
> That will produce a bunch of errors.
Only if the OP has *asked* for errors for doing bad things. If use
strict is not in effect, Perl has no problem taking the barewords
something
and
withaperiod
and treating them as two strings, which are concatenated together with
the . operator.
Paul Lalli
| |
| Tom Phoenix 2006-01-20, 6:59 pm |
| On 1/20/06, The Ghost <ghost@madisonip.com> wrote:
> I have keys with periods in them:
>
> my %hash;
> $hash{something.withaperiod}=3D"some text";
> my $something=3D'something';
> my $withaperiod=3D'withaperiod';
> print qq{$hash{"$something.$withaperiod"}\n};
>
>
> what will it print?
What did it print when you tried it?
See the perl docs for information about quoting strings. Hash keys
don't have to be quoted if they're barewords, but you can't have a
period in a bareword. Of course, there's to problem with having a
period in a string that's being used as a hash key. Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| Todd W 2006-01-20, 6:59 pm |
|
"The Ghost" <ghost@madisonip.com> wrote in message
news:022C5C73-93CB-498A-98B8-EDCF8AF2E749@madisonip.com...
> I have keys with periods in them:
>
> my %hash;
> $hash{something.withaperiod}="some text";
> my $something='something';
> my $withaperiod='withaperiod';
> print qq{$hash{"$something.$withaperiod"}\n};
>
>
> what will it print?
Looking at what you are trying to do here, I'm guessing you are trying to
"emulate" a complex data structure. You should just use one instead:
my %hash;
$hash{something}{withaperiod}="some text";
my $something='something';
my $withaperiod='withaperiod';
print qq{$hash{$something}{$withaperiod}\n};
That way you can use perl's built in constructs to iterate over the data
when you need to:
my $subhash = $hash{something};
foreach my $key ( keys %$subhash ) {
print("$key => $subhash->{$key}");
}
trwww
|
|
|
|