[32501] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3766 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 24 16:09:21 2012

Date: Fri, 24 Aug 2012 13:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 24 Aug 2012     Volume: 11 Number: 3766

Today's topics:
    Re: magic installation woes [OT] <npeelmandog@cfl.rr.com>
        Nice bear trap with $FOO file handle and $_ <dave@invalid.invalid>
    Re: Nice bear trap with $FOO file handle and $_ <ben@morrow.me.uk>
    Re: Nice bear trap with $FOO file handle and $_ <rweikusat@mssgmbh.com>
    Re: Nice bear trap with $FOO file handle and $_ <dave@invalid.invalid>
        Very simple hash/regex question <tuxedo@mailinator.com>
    Re: Very simple hash/regex question <klaus03@gmail.com>
    Re: Very simple hash/regex question <hjp-usenet2@hjp.at>
    Re: Very simple hash/regex question (Tim McDaniel)
    Re: Very simple hash/regex question <tuxedo@mailinator.com>
    Re: Very simple hash/regex question <tuxedo@mailinator.com>
    Re: Very simple hash/regex question <klaus03@gmail.com>
    Re: Very simple hash/regex question (Tim McDaniel)
    Re: Very simple hash/regex question <ben@morrow.me.uk>
    Re: Very simple hash/regex question <tuxedo@mailinator.com>
    Re: Very simple hash/regex question <ben@morrow.me.uk>
    Re: Very simple hash/regex question <tuxedo@mailinator.com>
    Re: Very simple hash/regex question (Tim McDaniel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 23 Aug 2012 09:56:23 -0400
From: Norman Peelman <npeelmandog@cfl.rr.com>
Subject: Re: magic installation woes [OT]
Message-Id: <k15cq8$ldj$1@dont-email.me>

On 08/22/2012 11:15 PM, Cal Dershowitz wrote:
> On 08/21/2012 04:07 AM, Cal Dershowitz wrote:
>> On 08/19/2012 11:31 PM, Cal Dershowitz wrote:
>>>
>>> Since you were missing a library named MagickCore and a header of the
>>> same name, the candidate suggesting itself would be libmagickcore-dev.
>>> You can install that with
>>>
>>> apt-get install libmagickcore-dev
>>
>>
>> $ sudo apt-get install libmagickcore-dev
>> [sudo] password for fred:
>> Reading package lists... Done
>> Building dependency tree
>> Reading state information... Done
>> libmagickcore-dev is already the newest version.
>> 0 upgraded, 0 newly installed, 0 to remove and 12 not upgraded.
>> $
>>
>> This is encouraging for me, because I've got 99 problems, and this is no
>> longer one of them.
>
> , he said, without successful output.
>
> fred@fred-desktop:~/Desktop$ sudo apt-get install imagemagick
> Reading package lists... Done
> Building dependency tree
> Reading state information... Done
> The following extra packages will be installed:
>    libmagickwand4 libnetpbm10 netpbm
> Suggested packages:
>    imagemagick-doc autotrace curl enscript ffmpeg gnuplot grads hp2xx
> html2ps
>    libwmf-bin mplayer povray radiance texlive-base-bin transfig ufraw-batch
> The following NEW packages will be installed:
>    imagemagick libmagickwand4 libnetpbm10 netpbm
> 0 upgraded, 4 newly installed, 0 to remove and 12 not upgraded.
> Need to get 1,611 kB of archives.
> After this operation, 5,030 kB of additional disk space will be used.
> Do you want to continue [Y/n]? y
> ....
>
> Q1)  What does it mean to be "processing triggers?"
>
> Processing triggers for man-db ...

   Setting up the info for 'man-db'... 'man' is a command-line program 
for reading program documentation (manuals). Open a terminal (make it 
fullscreen) and type:

fred@fred-desktop:~/Desktop$ man man

 ...for a description of 'man' using 'man'.

> Setting up libmagickwand4 (8:6.6.9.7-5ubuntu3.1) ...
> Setting up imagemagick (8:6.6.9.7-5ubuntu3.1) ...
> Setting up libnetpbm10 (2:10.0-15) ...
> Setting up netpbm (2:10.0-15) ...
> Processing triggers for libc-bin ...
> ldconfig deferred processing now taking place
>
> Q2)  What's that now?  ^^^^

   Processing all the triggers set up earlier... man-db updates, maybe 
putting entries in the menus, etc.


-- 
Norman
Registered Linux user #461062
AMD64X2 6400+ Ubuntu 10.04 64bit


------------------------------

Date: Thu, 23 Aug 2012 11:10:35 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Nice bear trap with $FOO file handle and $_
Message-Id: <fV45K0OBJxbE-pn2-h4VRdCEU7urn@localhost>

Just fell into a nice bear trap.

open FOO, '>', $bar or die "$!";
while ( <some file> )
{
  print FOO;
}

I was changing my scripts to using $HANDLE - AIUI this is s "Good 
Thing(TM)" although I am not sure why :-)

So the above print turned into "print $FOO;" - which of course does 
not default to printing $_.  It prints a GLOB to STDOUT.
-- 
Regards
Dave Saville


------------------------------

Date: Thu, 23 Aug 2012 15:06:29 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Nice bear trap with $FOO file handle and $_
Message-Id: <50qhg9-ej3.ln1@anubis.morrow.me.uk>


Quoth "Dave Saville" <dave@invalid.invalid>:
> Just fell into a nice bear trap.
> 
> open FOO, '>', $bar or die "$!";
> while ( <some file> )
> {
>   print FOO;
> }
> 
> I was changing my scripts to using $HANDLE - AIUI this is s "Good 
> Thing(TM)" although I am not sure why :-)

Suppose you are working on a large program. Also suppose that in your
program you have a function foo

    sub foo {
        ...
        open FOO, ">", $bar or...;
        bar();
        print FOO;
    }

and a function bar

    sub bar {
        ...
        open FOO, ">", $somewhere_else or...;
        my @lines = <FOO>;
        close FOO;
    }

foo will not work as expected, since bar will reopen and then close the
same filehandle as foo is using, but this is not at all obvious from
looking at foo itself. If they are in different files, or indeed if bar
is in some module you are using which you didn't write yourself, it may
take a while for you to work out where the problem is.

Using lexical filehandles prevents this. Like this:

    sub foo {
        open my $FOO, ...;
        bar();
        print $FOO $_;
    }

    sub bar {
        open my $FOO, ...;
        my @lines = <$FOO>;
    }

the two subs get separate filehandles, because of the 'my's, even though
the variables they are stored in happen to have the same name.

Ben



------------------------------

Date: Thu, 23 Aug 2012 15:46:54 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Nice bear trap with $FOO file handle and $_
Message-Id: <87pq6hwtr5.fsf@sapphire.mobileactivedefense.com>

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth "Dave Saville" <dave@invalid.invalid>:

[...]

>> I was changing my scripts to using $HANDLE - AIUI this is s "Good 
>> Thing(TM)" although I am not sure why :-)
>
> Suppose you are working on a large program. Also suppose that in your
> program you have a function foo
>
>     sub foo {
>         ...
>         open FOO, ">", $bar or...;
>         bar();
>         print FOO;
>     }
>
> and a function bar
>
>     sub bar {
>         ...
>         open FOO, ">", $somewhere_else or...;
>         my @lines = <FOO>;
>         close FOO;
>     }
>
> foo will not work as expected, since bar will reopen and then close the
> same filehandle as foo is using, but this is not at all obvious from
> looking at foo itself.

That's sort-of a disingenious example because just using

sub foo
{
    local *FOO;

and

sub bar
{
    local *FOO;

the old-style way for creating local file handles, would work fine
here. The downside of this is that using local changes the environment
for all subroutines called from the subroutine containing the
local: The newly created glob is implicitly accessible from all
subroutines called from either foo or bar (and to subroutines called
from these subroutines and so on) and it may even cause them to access
a different filehandle then the one they were supposed to
access. Because of this, local should be avoided except when this
particular phenomenon is actually the desired behaviour.


------------------------------

Date: Thu, 23 Aug 2012 16:23:27 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Nice bear trap with $FOO file handle and $_
Message-Id: <fV45K0OBJxbE-pn2-PH09xH4oIkoT@localhost>

On Thu, 23 Aug 2012 14:06:29 UTC, Ben Morrow <ben@morrow.me.uk> wrote:

> Suppose you are working on a large program. Also suppose that in your
> program you have a function foo
> 
>     sub foo {
>         ...
>         open FOO, ">", $bar or...;
>         bar();
>         print FOO;
>     }
> 
> and a function bar
> 
>     sub bar {
>         ...
>         open FOO, ">", $somewhere_else or...;
>         my @lines = <FOO>;
>         close FOO;
>     }
> 
> foo will not work as expected, since bar will reopen and then close the
> same filehandle as foo is using, but this is not at all obvious from
> looking at foo itself. If they are in different files, or indeed if bar
> is in some module you are using which you didn't write yourself, it may
> take a while for you to work out where the problem is.

Thanks for the explanation Ben. I *knew* there was a good reason :-)
-- 
Regards
Dave Saville


------------------------------

Date: Thu, 23 Aug 2012 20:03:26 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Very simple hash/regex question
Message-Id: <k15r9f$i5v$1@news.albasani.net>

What is a simple way to copy a hash into for example %hash_copy and change 
all characters in the keys of the copied hash to lowercases and all 
whitespaces to underscores?

my %hash = ('My first subject key' => 'my first value',
        'my second Subject key' => 'my second value');

my %hash_copy = %hash;

 ..?

Many thanks for any suggestions.

Tuxedo




------------------------------

Date: Thu, 23 Aug 2012 12:05:45 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: Re: Very simple hash/regex question
Message-Id: <3d7894ea-712f-44ba-b266-d72614f0a7c5@e29g2000vbm.googlegroups.com>

On 23 ao=FBt, 20:03, Tuxedo <tux...@mailinator.com> wrote:
> What is a simple way to copy a hash into for example %hash_copy and chang=
e
> all characters in the keys of the copied hash to lowercases and all
> whitespaces to underscores?

use 5.014;
my %hash =3D ('My first subject key' =3D> 'my first value',
        'my second Subject key' =3D> 'my second value');

my %hash_copy =3D map { lc($_ =3D~ s/ /_/gr) =3D> $hash{$_} } %hash;


------------------------------

Date: Thu, 23 Aug 2012 21:49:39 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Very simple hash/regex question
Message-Id: <slrnk3d2aj.71p.hjp-usenet2@hrunkner.hjp.at>

On 2012-08-23 19:05, Klaus <klaus03@gmail.com> wrote:
> On 23 août, 20:03, Tuxedo <tux...@mailinator.com> wrote:
>> What is a simple way to copy a hash into for example %hash_copy and change
>> all characters in the keys of the copied hash to lowercases and all
>> whitespaces to underscores?
>
> use 5.014;
> my %hash = ('My first subject key' => 'my first value',
>         'my second Subject key' => 'my second value');
>
> my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;

use Data::Dumper;
say Dumper \%hash_copy;


$VAR1 = {
          'my_first_subject_key' => 'my first value',
          'my_second_value' => undef,
          'my_first_value' => undef,
          'my_second_subject_key' => 'my second value'
        };

not quite what Tuxedo wanted, I think.

	hp


-- 
   _  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         | 
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org


------------------------------

Date: Thu, 23 Aug 2012 20:22:14 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Very simple hash/regex question
Message-Id: <k163dm$ahe$1@reader1.panix.com>

In article <3d7894ea-712f-44ba-b266-d72614f0a7c5@e29g2000vbm.googlegroups.com>,
Klaus  <klaus03@gmail.com> wrote:
>On 23 août, 20:03, Tuxedo <tux...@mailinator.com> wrote:
>> What is a simple way to copy a hash into for example %hash_copy and change
>> all characters in the keys of the copied hash to lowercases and all
>> whitespaces to underscores?
>
>use 5.014;
>my %hash = ('My first subject key' => 'my first value',
>        'my second Subject key' => 'my second value');
>
>my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;

Fundamental problem 0: You didn't test the proposal.

1: %hash returns both keys and values, so hash_copy would get two
hashes for each one in the original table, one of them being
"transformed value => undef".  You want "map{...}keys %hash".

2: I don't believe Perl defines an order of operations in this case,
where one part of the expression modifies $_ and another part uses
it.  If it evaluates left to right, then $hash{$_} will try to use the
transformed $_, so it won't find the value.  (This bit me on my first
attempt too.)

3: The spec is "all whitespaces".  I think that means \s, not ' '.

4: "$_=~..." is the default operand.

My correction to that:

use 5.014;
my %hash_copy = map { my $key = $_; lc(s/\s/_/gr) => $hash{$key} } keys %hash;

But since you need a temp anyway (or so I think), there's no need for
s///r, so no need to require 5.014.  So this also works without 5.014:

my %hash_copy = map { my $orig_ = $_; s/\s/_/g; lc($_) => $hash{$orig_} } keys %hash;

On the whole, I think this looks cleaner with just a plain loop:

my %hash_copy = ();
while (my ($key, $value) = each %hash) {
    $key =~ s/\s/_/g;
    $hash_copy{lc($key)} = $value;
}


-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Thu, 23 Aug 2012 22:44:51 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Very simple hash/regex question
Message-Id: <k164o4$6bm$1@news.albasani.net>

Peter J. Holzer wrote:

> On 2012-08-23 19:05, Klaus <klaus03@gmail.com> wrote:
> > On 23 août, 20:03, Tuxedo <tux...@mailinator.com> wrote:
> >> What is a simple way to copy a hash into for example %hash_copy and
> >> change all characters in the keys of the copied hash to lowercases and
> >> all whitespaces to underscores?
> >
> > use 5.014;
> > my %hash = ('My first subject key' => 'my first value',
> >         'my second Subject key' => 'my second value');
> >
> > my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;
> 
> use Data::Dumper;
> say Dumper \%hash_copy;
> 
> 
> $VAR1 = {
>           'my_first_subject_key' => 'my first value',
>           'my_second_value' => undef,
>           'my_first_value' => undef,
>           'my_second_subject_key' => 'my second value'
>         };
> 
> not quite what Tuxedo wanted, I think.
> 
> hp
> 
> 

I'm not quite sure either to be honest....

What I have so far is a hash, like:

my %hash = ('My first subject key' => 'my first value',
            'my second Subject key' => 'my second value');

First I thought I should duplicate the hash into a copy named for example 
%hash_copy. Then modify the keys, not the values.

I would then run the script via cgi parameters, using the keys as they 
appear in the lowercase and with underscores, e.g. 'my_second_subject_key'. 

use CGI qw(param);
my $subject = param('subject');

The original %hash keys can contain spaces and capitals.

I would then like to access and print the key string in its original 
whitespace and partly uppercase format, e.g. 'my second Subject key' when 
accessing the script by for example.pl?subject=my_second_subject_key

Maybe it will be necessary to know the position of the given key in the 
%hash_copy in order to access the key string in the same numerical position 
as in the original %hash?

The idea is simply to access and print an original key string, based on the 
modified one in the query string, without unecessarily resorting to the 
idea of maintaining near duplicate hashes manually....

As mentioned, I'm not quite sure which is the best way to go about this. 

Many thanks for any ideas.

Tuxedo




------------------------------

Date: Thu, 23 Aug 2012 22:57:28 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Very simple hash/regex question
Message-Id: <k165fp$7oi$1@news.albasani.net>

Tim McDaniel wrote:

> In article
> <3d7894ea-712f-44ba-b266-d72614f0a7c5@e29g2000vbm.googlegroups.com>,
> Klaus  <klaus03@gmail.com> wrote:
> >On 23 août, 20:03, Tuxedo <tux...@mailinator.com> wrote:
> >> What is a simple way to copy a hash into for example %hash_copy and
> >> change all characters in the keys of the copied hash to lowercases and
> >> all whitespaces to underscores?
> >
> >use 5.014;
> >my %hash = ('My first subject key' => 'my first value',
> >        'my second Subject key' => 'my second value');
> >
> >my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;
> 
> Fundamental problem 0: You didn't test the proposal.
> 
> 1: %hash returns both keys and values, so hash_copy would get two
> hashes for each one in the original table, one of them being
> "transformed value => undef".  You want "map{...}keys %hash".
> 
> 2: I don't believe Perl defines an order of operations in this case,
> where one part of the expression modifies $_ and another part uses
> it.  If it evaluates left to right, then $hash{$_} will try to use the
> transformed $_, so it won't find the value.  (This bit me on my first
> attempt too.)
> 
> 3: The spec is "all whitespaces".  I think that means \s, not ' '.
> 
> 4: "$_=~..." is the default operand.
> 
> My correction to that:
> 
> use 5.014;
> my %hash_copy = map { my $key = $_; lc(s/\s/_/gr) => $hash{$key} } keys
> %hash;
> 
> But since you need a temp anyway (or so I think), there's no need for
> s///r, so no need to require 5.014.  So this also works without 5.014:
> 
> my %hash_copy = map { my $orig_ = $_; s/\s/_/g; lc($_) => $hash{$orig_} }
> keys %hash;
> 
> On the whole, I think this looks cleaner with just a plain loop:
> 
> my %hash_copy = ();
> while (my ($key, $value) = each %hash) {
>     $key =~ s/\s/_/g;
>     $hash_copy{lc($key)} = $value;
> }
> 
> 

Thanks for posting these examples. I will test.

Tuxedo


------------------------------

Date: Thu, 23 Aug 2012 14:35:39 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: Re: Very simple hash/regex question
Message-Id: <15633a57-084e-4b46-8bec-77326cf25201@k20g2000vbk.googlegroups.com>

On 23 ao=FBt, 22:22, t...@panix.com (Tim McDaniel) wrote:
> In article <3d7894ea-712f-44ba-b266-d72614f0a...@e29g2000vbm.googlegroups=
 .com>,
>
> Klaus =A0<klau...@gmail.com> wrote:
> >On 23 ao=FBt, 20:03, Tuxedo <tux...@mailinator.com> wrote:
> >> What is a simple way to copy a hash into for example %hash_copy and ch=
ange
> >> all characters in the keys of the copied hash to lowercases and all
> >> whitespaces to underscores?
>
> >use 5.014;
> >my %hash =3D ('My first subject key' =3D> 'my first value',
> > =A0 =A0 =A0 =A0'my second Subject key' =3D> 'my second value');
>
> >my %hash_copy =3D map { lc($_ =3D~ s/ /_/gr) =3D> $hash{$_} } %hash;
>
> Fundamental problem 0: You didn't test the proposal.

doh and double-doh, I typed perl code on the fly and I messed up !

You (and Peter J. Holzer) are of course right. I didn't test my code
and I apologise.


------------------------------

Date: Thu, 23 Aug 2012 21:44:24 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Very simple hash/regex question
Message-Id: <k1687o$n13$1@reader1.panix.com>

In article <k164o4$6bm$1@news.albasani.net>,
Tuxedo  <tuxedo@mailinator.com> wrote:
>Peter J. Holzer wrote:
>> not quite what Tuxedo wanted, I think.
>
>I'm not quite sure either to be honest....

Well, it's hard to get to a desired destination when you don't know
where you're going ...

>What I have so far is a hash, like:
>
>my %hash = ('My first subject key' => 'my first value',
>            'my second Subject key' => 'my second value');
>
>First I thought I should duplicate the hash into a copy named for example 
>%hash_copy. Then modify the keys, not the values.

Well, to be precise, you can't per se modify the key of a hash
element.  You can get the effect of that by creating a new hash
member, assigning an older value to be its value, then deleting the
older key and its value.

>I would then run the script via cgi parameters, using the keys as they 
>appear in the lowercase and with underscores, e.g. 'my_second_subject_key'. 
>
>use CGI qw(param);
>my $subject = param('subject');
>
>The original %hash keys can contain spaces and capitals.
>I would then like to access and print the key string in its original
>whitespace and partly uppercase format, e.g. 'my second Subject key'
>when accessing the script by for
>example.pl?subject=my_second_subject_key

Had it been a case of needing to go from 'my second Subject key' to
'my_second_subject_key', you could use a hash or a sub.  However, the
other direction is one to many -- given 'my_second_subject_key', you
can't tell what the original was.  So you'd have to use a hash.  In
any event, you have to determine whether it's possible to have a
collision like "my second Subject key" and "mY SeCond\tSUBJECT\rKeY",
and if so, what you plan to do about it.

>Maybe it will be necessary to know the position of the given key in
>the %hash_copy in order to access the key string in the same
>numerical position as in the original %hash?

You cannot access a hash by a numerical position.  You can only go
directly to an element via its key.

>The idea is simply to access and print an original key string, based
>on the modified one in the query string, without unecessarily
>resorting to the idea of maintaining near duplicate hashes
>manually....

Well, sorry, but that's what you're going to have to do.

If you are planning to do changes and references in lots of places,
then you might encapsulate the tracking in a module, or even a class,
with map_original_to_normalized(), map_normalized_to_original(), add,
delete, and such.

-- 
Tim McDaniel, tmcd@panix.com



------------------------------

Date: Thu, 23 Aug 2012 22:57:45 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Very simple hash/regex question
Message-Id: <pjlig9-r06.ln1@anubis.morrow.me.uk>


Quoth tmcd@panix.com:
> In article <3d7894ea-712f-44ba-b266-d72614f0a7c5@e29g2000vbm.googlegroups.com>,
> Klaus  <klaus03@gmail.com> wrote:
> >
> >my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;
<snip>
> 
> 2: I don't believe Perl defines an order of operations in this case,
> where one part of the expression modifies $_ and another part uses
> it.  If it evaluates left to right, then $hash{$_} will try to use the
> transformed $_, so it won't find the value.  (This bit me on my first
> attempt too.)

I believe the order of operations is always well-defined in Perl: that
is, I don't know of any cases where it's been changed, nor any cases
where changing the order wouldn't be considered a bug. In this case,
'=>' is just sugar for ',', so the order would be well-defined even in
C.

Ben



------------------------------

Date: Fri, 24 Aug 2012 14:09:24 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Very simple hash/regex question
Message-Id: <k17qtl$425$1@news.albasani.net>

Tim McDaniel wrote:

[...]

> collision like "my second Subject key" and "mY SeCond\tSUBJECT\rKeY",
> and if so, what you plan to do about it.

Thanks for mentioning this, it could indeed happen.

[...]

> You cannot access a hash by a numerical position.  You can only go
> directly to an element via its key.

I suspected as much, so I'm not sure how it can be done.

> >The idea is simply to access and print an original key string, based
> >on the modified one in the query string, without unecessarily
> >resorting to the idea of maintaining near duplicate hashes
> >manually....
> 
> Well, sorry, but that's what you're going to have to do.

All I had planned was to keep normal key values as they would be written in 
a natural language, then change them to a format which contains no spaces 
or capitals, then access both the original (normalised) key strings and 
values as well as the modified ones with underscores, while only knowing 
the modified key string at the time the script runs on a CGI request. 
Instead, to maintain two separate hashes which can be accessed by the same 
key-string in the query string can of course be done. It just means 
dublicating some information manually, which is no big deal, although 
suspect it can be done better. Anyway, then there would be the main hash:

my %hash = ('my_first_subject_key' => 'my first value',
        'my_second_subject key' => 'my second value');

And an additional hash, providing the normalised word strings as values:

my %hash_normalize = ('my_first_subject_key' => 'My first subject key',
                'my_second_subject_key' => 'my second Subject key');

I can now access both normal and modified versions using one parameter as a 
key to both hashes.

> If you are planning to do changes and references in lots of places,
> then you might encapsulate the tracking in a module, or even a class,
> with map_original_to_normalized(), map_normalized_to_original(), add,
> delete, and such.

I'm not sure what kind of tracking module you refer to? Also, I don't fully 
understand the map_original_to_normalized() and 
map_normalized_to_original() class ideas.

Or maybe some other data structure could be better suited for my purpose.
 
Many thanks,
Tuxedo


------------------------------

Date: Fri, 24 Aug 2012 18:20:38 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Very simple hash/regex question
Message-Id: <6opkg9-rrh.ln1@anubis.morrow.me.uk>


Quoth Tuxedo <tuxedo@mailinator.com>:
> 
> All I had planned was to keep normal key values as they would be written in 
> a natural language, then change them to a format which contains no spaces 
> or capitals, then access both the original (normalised) key strings and 
> values as well as the modified ones with underscores, while only knowing 
> the modified key string at the time the script runs on a CGI request. 
> Instead, to maintain two separate hashes which can be accessed by the same 
> key-string in the query string can of course be done. It just means 
> dublicating some information manually, which is no big deal, although 
> suspect it can be done better. Anyway, then there would be the main hash:
> 
> my %hash = ('my_first_subject_key' => 'my first value',
>         'my_second_subject key' => 'my second value');
> 
> And an additional hash, providing the normalised word strings as values:
> 
> my %hash_normalize = ('my_first_subject_key' => 'My first subject key',
>                 'my_second_subject_key' => 'my second Subject key');
> 
> I can now access both normal and modified versions using one parameter as a 
> key to both hashes.

I would structure this like this:

    my %hash = (
        my_first_subject_key => {
            key     => "My first subject key",
            value   => "my first value",
        },
        ...
    );

See perllol, perldsc and perlreftut. Of course, you might want to give
the subhashes more meaningful keys than 'key' and 'value'.

Ben



------------------------------

Date: Fri, 24 Aug 2012 20:39:23 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Very simple hash/regex question
Message-Id: <k18hos$i8m$1@news.albasani.net>

Ben Morrow wrote:

[...]

> I would structure this like this:
> 
>     my %hash = (
>         my_first_subject_key => {
>             key     => "My first subject key",
>             value   => "my first value",
>         },
>         ...
>     );
> 
> See perllol, perldsc and perlreftut. Of course, you might want to give
> the subhashes more meaningful keys than 'key' and 'value'.
> 
> Ben
> 

Thanks for the example. I will delve into those manuals.

Tuxedo


------------------------------

Date: Fri, 24 Aug 2012 19:46:52 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Very simple hash/regex question
Message-Id: <k18lnc$nl0$1@reader1.panix.com>

In article <pjlig9-r06.ln1@anubis.morrow.me.uk>,
Ben Morrow  <ben@morrow.me.uk> wrote:
>
>Quoth tmcd@panix.com:
>> In article <3d7894ea-712f-44ba-b266-d72614f0a7c5@e29g2000vbm.googlegroups.com>,
>> Klaus  <klaus03@gmail.com> wrote:
>> >
>> >my %hash_copy = map { lc($_ =~ s/ /_/gr) => $hash{$_} } %hash;
><snip>
>> 
>> 2: I don't believe Perl defines an order of operations in this case,
>> where one part of the expression modifies $_ and another part uses
>> it.  If it evaluates left to right, then $hash{$_} will try to use the
>> transformed $_, so it won't find the value.  (This bit me on my first
>> attempt too.)
>
>I believe the order of operations is always well-defined in Perl: that
>is, I don't know of any cases where it's been changed, nor any cases
>where changing the order wouldn't be considered a bug.

TL;DR: show me where Perl systematically talks about order of
evaluation, except implicitly in some places, or talks about anything
like "sequence points".

I don't know of any place that Perl explicitly defines the order of
evaluation and refers to anything like C's "sequence points", except
where implied by things like "If the argument before the ? is true,
the argument before the : is returned, otherwise the argument after
the : is returned.".  For example, for ++ and --, man perlop has

    Note that just as in C, Perl doesn't define when the variable is
    incremented or decremented. You just know it will be done sometime
    before or after the value is returned. This also means that
    modifying a variable twice in the same statement will lead to
    undefined behavior.  Avoid statements like:

        $i = $i ++;
        print ++ $i + $i ++;

    Perl will not guarantee what the result of the above statements
    is.

But C actually *does* define when the increment or decrement happens:
some time after the previous sequence point and before the next one.
C would not have sequence points in the problematic areas above, mind
you, so that wouldn't matter in these two lines.  But C does define
that in
     ... (i++, i) ...
the increment happens no later than the comma operator, so the value
of "i" alone is the incremented version.  (If I'm reading a draft
standard right, if it matches the current version, and if my old
neurons are firing right.)

>In this case, '=>' is just sugar for ',', so the order would be
>well-defined even in C.

No it would not.  In the map above, the tokens "=>" and "," are not
comma operators, which in C would cause a sequence point.  The only
place I know of in C where "," represents a list of values is in
initializations of arrays or structs or the like, and the draft I saw
(can't find the real standard) has "The evaluations of the
initialization list expressions are indeterminately sequenced with
respect to one another and thus the order in which any side effects
occur is unspecified."

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3766
***************************************


home help back first fref pref prev next nref lref last post