Home > Archive > PERL Beginners > March 2005 > tricky hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ing. Branislav Gerzo 2005-03-08, 3:57 pm |
| Hello beginners@perl.org,
anyone could me help with:
use strict;
use warnings;
my %kw = ();
my $kw = \%kw;
for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++; $kw->{cat${kw->{cnt}}} = $cat }
I get: Can't use bareword ("kw") as a HASH ref while "strict refs" in
use..
The goal is to get something like:
$kw->{cat1} = 'something',
$kw->{cat2} = 'other',
$kw->{cat3} = 'foo',
$kw->{cat4} = 'bar'
anyone?
--
--. ,-- ,- ICQ: 7552083 \|||/ `//EB: www.2ge.us
,--' | - |-- IRC: [2ge] (. .) ,\\SN: 2ge!2ge_us
`====+==+=+===~ ~=============-o00-(_)-00o-================~
When hell freezes over? I just sold Satan a fur coat the other day...
| |
| Manav Mathur 2005-03-08, 3:57 pm |
|
use strict;
use warnings;
use Data::Dumper ;
my %kw = ();
my $kw = \%kw;
my $i = 1;
for my $cat (split(/\//, 'something/other/foo/bar'))
{
$kw->{"cat".eval{$i++}} = $cat
}
print Dumper \%kw ;
-----Original Message-----
From: Ing. Branislav Gerzo [mailto:konfera@2ge.us]
Sent: Tuesday, March 08, 2005 10:14 PM
To: beginners@perl.org
Subject: tricky hash
Hello beginners@perl.org,
anyone could me help with:
use strict;
use warnings;
my %kw = ();
my $kw = \%kw;
for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++;
$kw->{cat${kw->{cnt}}} = $cat }
I get: Can't use bareword ("kw") as a HASH ref while "strict refs" in
use..
The goal is to get something like:
$kw->{cat1} = 'something',
$kw->{cat2} = 'other',
$kw->{cat3} = 'foo',
$kw->{cat4} = 'bar'
anyone?
--
--. ,-- ,- ICQ: 7552083 \|||/ `//EB: www.2ge.us
,--' | - |-- IRC: [2ge] (. .) ,\\SN: 2ge!2ge_us
`====+==+=+===~ ~=============-o00-(_)-00o-================~
When hell freezes over? I just sold Satan a fur coat the other day...
--
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>
****************************************
*****************
Disclaimer:
The contents of this E-mail (including the contents of the enclosure(s) or attachment(s) if any) are privileged and confidential material of MBT and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee(s)
. In case you are not the desired addressee, you should delete this message and/or re-direct it to the sender. The views expressed in this E-mail message (including the enclosure(s) or attachment(s) if any) are those of the individual sender, except wh
ere the sender expressly, and with authority, states them to be the views of MBT.
This e-mail message including attachment/(s), if any, is believed to be free of any virus. However, it is the responsibility of the recipient to ensure that it is virus free and MBT is not responsible for any loss or damage arising in any way from its us
e
****************************************
*****************
| |
| Wiggins d'Anconia 2005-03-08, 3:57 pm |
| Ing. Branislav Gerzo wrote:
> Hello beginners@perl.org,
>
> anyone could me help with:
>
> use strict;
> use warnings;
> my %kw = ();
> my $kw = \%kw;
> for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++; $kw->{cat${kw->{cnt}}} = $cat }
You don't have to accept the rope Perl will give you to hang yourself,
$kw->{'cat'.$kw->{'cnt'}} = $cat;
The special quoting Perl does on hash keys is really only useful for
simple strings.
Perl also ignores whitespace for a reason, consider not writing whole
programs on one line, it will help you with debugging.
>
>
> I get: Can't use bareword ("kw") as a HASH ref while "strict refs" in
> use..
> The goal is to get something like:
> $kw->{cat1} = 'something',
> $kw->{cat2} = 'other',
> $kw->{cat3} = 'foo',
> $kw->{cat4} = 'bar'
>
> anyone?
>
http://danconia.org
| |
| Ankur Gupta 2005-03-08, 3:57 pm |
| Ing. Branislav Gerzo wrote:
>Hello beginners@perl.org,
>
>anyone could me help with:
>
>use strict;
>use warnings;
>my %kw = ();
>my $kw = \%kw;
>for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++; $kw->{cat${kw->{cnt}}} = $cat }
>
Change this line to
for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++; $kw->{"cat" . $kw->{cnt}} = $cat }
This should work.
| |
| Lawrence Statton 2005-03-08, 3:57 pm |
| > Hello beginners@perl.org,
>
>
> I get: Can't use bareword ("kw") as a HASH ref while "strict refs" in
> use..
> The goal is to get something like:
> $kw->{cat1} = 'something',
> $kw->{cat2} = 'other',
> $kw->{cat3} = 'foo',
> $kw->{cat4} = 'bar'
>
Well, fixing the typo where you build the key for the last assignment
would help with your immediate problem. *HOWEVER* You're still
solving the problem in a wrong-way.
*EVERY TIME* you see a list of variable names (or hash keys) of the
form foo1 foo2 foo3 foo4 that should be a GIANT WAVING RED FLAG that
your data model is lacking.
Lacking is not strong enough a word.
How about "is pessimally awful."
"...deserves never to see the light of day."
Perl has a built in data type highly suited for a series of data
indexed by a monotonically increasing integer. The List.
my @keyword = split ( /\//, 'something/wicked/this/way/comes' ) ;
If you like the refence version of it
my $keywords = [ split (/\//, 'something/wicked/this/way/comes') ] ;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
| |
| John W. Krahn 2005-03-09, 3:56 am |
| Ing. Branislav Gerzo wrote:
> Hello beginners@perl.org,
Hello,
> anyone could me help with:
>
> use strict;
> use warnings;
> my %kw = ();
> my $kw = \%kw;
> for my $cat (split(/\//, 'something/other/foo/bar')) { $kw->{cnt}++; $kw->{cat${kw->{cnt}}} = $cat }
>
>
> I get: Can't use bareword ("kw") as a HASH ref while "strict refs" in
> use..
> The goal is to get something like:
> $kw->{cat1} = 'something',
> $kw->{cat2} = 'other',
> $kw->{cat3} = 'foo',
> $kw->{cat4} = 'bar'
>
> anyone?
my $string = 'something/other/foo/bar';
my $count;
my $kw = { map { 'cat' . ++$count, $_ } split /\//, $string };
John
--
use Perl;
program
fulfillment
| |
| Ing. Branislav Gerzo 2005-03-09, 3:56 am |
| John W. Krahn [JWK], on Tuesday, March 08, 2005 at 12:58 (-0800) made
these points:
JWK> my $string = 'something/other/foo/bar';
JWK> my $count;
JWK> my $kw = { map { 'cat' . ++$count, $_ } split /\//, $string };
this is really tricky reply I awaited, I like this solution, really
nice. Also, in my case, the best is:
use strict;
use warnings;
use Data::Dumper ;
my %kw = ();
my $kw = \%kw;
$kw = { map { 'cat' . ++$kw->{cnt}, $_ } split /\//, 'something/other/foo/bar' };
print Dumper $kw;
__END__
$VAR1 = {
'cat2' => 'other',
'cat3' => 'foo',
'cat1' => 'something',
'cat4' => 'bar'
};
Interesting on this is, $kw->{cnt} doesn't exist :)
Thanks to all who replied!
--
...m8s, cu l8r, Brano.
[Is a white person from Africa an African-American?]
|
|
|
|
|