[29553] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 797 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 26 14:14:14 2007

Date: Sun, 26 Aug 2007 11:14:06 -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           Sun, 26 Aug 2007     Volume: 11 Number: 797

Today's topics:
        Why can't I access an Array like this? <bill@ts1000.us>
    Re: Why can't I access an Array like this? <jurgenex@hotmail.com>
    Re: Why can't I access an Array like this? <noreply@gunnar.cc>
    Re: Why can't I access an Array like this? <bill@ts1000.us>
    Re: Why can't I access an Array like this? xhoster@gmail.com
    Re: Why can't I access an Array like this? <rkb@i.frys.com>
    Re: Why can't I access an Array like this? <rkb@i.frys.com>
    Re: Why can't I access an Array like this? <bill@ts1000.us>
    Re: Why can't I access an Array like this? <jurgenex@hotmail.com>
    Re: Why can't I access an Array like this? <jurgenex@hotmail.com>
    Re: Why can't I access an Array like this? <bik.mido@tiscalinet.it>
    Re: Why can't I access an Array like this? <bik.mido@tiscalinet.it>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 26 Aug 2007 05:16:32 -0700
From:  Bill H <bill@ts1000.us>
Subject: Why can't I access an Array like this?
Message-Id: <1188130592.477791.98580@r34g2000hsd.googlegroups.com>

This has always vexed me. When trying to access an array in the
following manner it never works and I end up assigning a variable. Can
someone point me to some docs on what I am doing wrong?

Example to show the issue, not my real code:

for($i = 0;$i < 4;$i++)
{
$temp = substr("0000".$i,-4);
$in{'VALUE$i'} = $i; # This never works
}

The $in{'VALUE$i'} never works, I have tried a number of different
ways:

$in{"VALUE$i"}
$in{"VALUE".$i}
ect

The only thing that seems to work is:

$a = "VALUE$i";
$in{$a} = $i;

But this can be cumbersome when I have a lot of variables I am trying
to set. I am sure there is some simple perl way of doing this, I just
have never found it yet.

Bill H



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

Date: Sun, 26 Aug 2007 12:33:39 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Why can't I access an Array like this?
Message-Id: <DqeAi.4149$yv3.3087@trndny01>

Bill H wrote:
> This has always vexed me. When trying to access an array in the
> following manner it never works and I end up assigning a variable. Can
> someone point me to some docs on what I am doing wrong?
>
> Example to show the issue, not my real code:
>
> for($i = 0;$i < 4;$i++)

for my $i (0..3)

> {
> $temp = substr("0000".$i,-4);
> $in{'VALUE$i'} = $i; # This never works

There is no array here. Did you mean
    $in['VALUE$i']
instead? 'VALUE$i' is the literal string without $i expanded. The numerical 
value of that string would be 0. So that statement would be the same as
    $in[0].
regardless of the value of $i.

jue 




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

Date: Sun, 26 Aug 2007 16:23:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Why can't I access an Array like this?
Message-Id: <5jdgnbF3r4lifU1@mid.individual.net>

Bill H wrote:
> 
> for($i = 0;$i < 4;$i++)
> {
> $temp = substr("0000".$i,-4);
> $in{'VALUE$i'} = $i; # This never works
> }
> 
> The $in{'VALUE$i'} never works,

Sure it does. It assigns to the hash key 'VALUE$i' - not expanded - 
every time.

> I have tried a number of different ways:
> 
> $in{"VALUE$i"}
> $in{"VALUE".$i}

Please post a short but complete program to show us what problem you 
have with those.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 26 Aug 2007 08:39:07 -0700
From:  Bill H <bill@ts1000.us>
Subject: Re: Why can't I access an Array like this?
Message-Id: <1188142747.945382.248900@k79g2000hse.googlegroups.com>

On Aug 26, 10:23 am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> Bill H wrote:
>
> > for($i = 0;$i < 4;$i++)
> > {
> > $temp = substr("0000".$i,-4);
> > $in{'VALUE$i'} = $i; # This never works
> > }
>
> > The $in{'VALUE$i'} never works,
>
> Sure it does. It assigns to the hash key 'VALUE$i' - not expanded -
> every time.
>
> > I have tried a number of different ways:
>
> > $in{"VALUE$i"}
> > $in{"VALUE".$i}
>
> Please post a short but complete program to show us what problem you
> have with those.
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

Here is a simple working example of what I am trying to do:

$in{'TEST0001'} = "Hello 0001";
$in{'TEST0002'} = "Hello 0002";
$in{'TEST0003'} = "Hello 0003";
$in{'TEST0004'} = "Hello 0004";


for($i = 1;$i < 5;$i++)
{
    $a = substr("0000".$i,-4);
    print "TEST$a = $in{'TEST$a'}\n";
}

for($i = 1;$i < 5;$i++)
{
    $a = substr("0000".$i,-4);
    $b = "TEST$a";
    print "$b = $in{$b}\n";
}


The 1st for loop doesnt work as expected, but the second does.

Here is the output:

TEST0001 =
TEST0002 =
TEST0003 =
TEST0004 =
TEST0001 = Hello 0001
TEST0002 = Hello 0002
TEST0003 = Hello 0003
TEST0004 = Hello 0004


I am not sure why I need to go to the extra step of assigning a
variable (in this case $b) to access the data.

Bill H



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

Date: 26 Aug 2007 16:09:33 GMT
From: xhoster@gmail.com
Subject: Re: Why can't I access an Array like this?
Message-Id: <20070826120935.412$TY@newsreader.com>

Bill H <bill@ts1000.us> wrote:
> On Aug 26, 10:23 am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> >
> > Please post a short but complete program to show us what problem you
> > have with those.
> >
>
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
>     $a = substr("0000".$i,-4);
>     print "TEST$a = $in{'TEST$a'}\n";
> }

You are using non-interpolating quotes for the hash key, so not
surprisingly they don't interpolate.  You need to use interpolating
quotes, and need ones that don't interfere with the outer quotes,
such as this:

print "TEST$a = $in{qq'TEST$a'}\n";

But once we are using qq construct, I'd prefer to change the character
that goes with it:

print "TEST$a = $in{qq{TEST$a}}\n";

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Sun, 26 Aug 2007 09:17:11 -0700
From:  Ron Bergin <rkb@i.frys.com>
Subject: Re: Why can't I access an Array like this?
Message-Id: <1188145031.089756.173400@e9g2000prf.googlegroups.com>

On Aug 26, 8:39 am, Bill H <b...@ts1000.us> wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
>     $a = substr("0000".$i,-4);
>     print "TEST$a = $in{'TEST$a'}\n";
>
> }
Due to the single quotes, you won't get the needed variable
interpolation.
>
> for($i = 1;$i < 5;$i++)
> {
>     $a = substr("0000".$i,-4);
>     $b = "TEST$a";
>     print "$b = $in{$b}\n";
>
> }
>
> The 1st for loop doesnt work as expected, but the second does.
>
> Here is the output:
>
> TEST0001 =
> TEST0002 =
> TEST0003 =
> TEST0004 =
> TEST0001 = Hello 0001
> TEST0002 = Hello 0002
> TEST0003 = Hello 0003
> TEST0004 = Hello 0004
>
> I am not sure why I need to go to the extra step of assigning a
> variable (in this case $b) to access the data.
>
You wouldn't need to if you used proper quoting.

for($i = 1;$i < 5;$i++)
{
    $a = substr("0000".$i,-4);
    print qq(TEST$a = $in{"TEST$a"}\n);
}

My preference would be to write it like this:

for my $i (1..4) {
    my $key = sprintf("TEST%0.4d", $i);
    print "$key = $in{$key}\n";
}



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

Date: Sun, 26 Aug 2007 09:34:28 -0700
From:  Ron Bergin <rkb@i.frys.com>
Subject: Re: Why can't I access an Array like this?
Message-Id: <1188146068.467804.172400@q3g2000prf.googlegroups.com>

On Aug 26, 8:39 am, Bill H <b...@ts1000.us> wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
>     $a = substr("0000".$i,-4);
>     $b = "TEST$a";
>     print "$b = $in{$b}\n";
>
> }
>
Since you're working with a hash, why not simply iterate over it with
"each"?

foreach my $key ( sort keys %in ) {
    print "$key = $in{$key}\n";
}



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

Date: Sun, 26 Aug 2007 09:45:34 -0700
From:  Bill H <bill@ts1000.us>
Subject: Re: Why can't I access an Array like this?
Message-Id: <1188146734.696611.131580@k79g2000hse.googlegroups.com>

On Aug 26, 12:09 pm, xhos...@gmail.com wrote:
> Bill H <b...@ts1000.us> wrote:
> > On Aug 26, 10:23 am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
>
> > > Please post a short but complete program to show us what problem you
> > > have with those.
>
> > Here is a simple working example of what I am trying to do:
>
> > $in{'TEST0001'} = "Hello 0001";
> > $in{'TEST0002'} = "Hello 0002";
> > $in{'TEST0003'} = "Hello 0003";
> > $in{'TEST0004'} = "Hello 0004";
>
> > for($i = 1;$i < 5;$i++)
> > {
> >     $a = substr("0000".$i,-4);
> >     print "TEST$a = $in{'TEST$a'}\n";
> > }
>
> You are using non-interpolating quotes for the hash key, so not
> surprisingly they don't interpolate.  You need to use interpolating
> quotes, and need ones that don't interfere with the outer quotes,
> such as this:
>
> print "TEST$a = $in{qq'TEST$a'}\n";
>
> But once we are using qq construct, I'd prefer to change the character
> that goes with it:
>
> print "TEST$a = $in{qq{TEST$a}}\n";
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> Usenet Newsgroup Service                        $9.95/Month 30GB- Hide quoted text -
>
> - Show quoted text -

Xho,

I knew there was some way of doing it Thanks for pointing out using
the qq construct!

Ron,

I use the keys hash alot, but for this usage I needed to do it
differently. Basically what the processor is doing is reading in form
values and all I know about the form values is that there are 10
fields that start with different words and end in ???? where ???? is
0001 - 9999. I also know how many groups of these values there are so
I have to do a for loop to read in the values for each group based on
the starting word and a counter, checking that some of the groups
aren't empty etc.

Bill H



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

Date: Sun, 26 Aug 2007 16:59:02 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Why can't I access an Array like this?
Message-Id: <qjiAi.4165$yv3.2338@trndny01>

Bill H wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";

As I mentioned in an earlier reply you don't have arrays. Those are hashes.

> for($i = 1;$i < 5;$i++)

Better written as
    for my $i (1..4)

> {
>    $a = substr("0000".$i,-4);
>    print "TEST$a = $in{'TEST$a'}\n";

I you had used strict and warnings this would have generated
    Use of uninitialized value in concatenation (.) or string at ...
As others have pointed out using single quotes doesn't interpolate the 
variable, so you were looking for the literal key 'TEST$a' which doesn't 
exist, of course.

It appears that
    print "TEST$a =" . $in{"TEST$a"} . "\n";
will do what you seem to be looking for.

But looping through a hash is much easier done with
    for (keys %in) {
        print "$in{$_}\n";
    }

Also using consecutively numbered variables (or hash keys in this case) 
usually is a strong indicator that you are using the wrong data structure. A 
hash of array would probably be a better choice.

jue 




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

Date: Sun, 26 Aug 2007 17:18:41 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Why can't I access an Array like this?
Message-Id: <RBiAi.3006$XV2.2413@trndny09>

Bill H wrote:
> I use the keys hash alot, but for this usage I needed to do it
> differently. Basically what the processor is doing is reading in form
> values and all I know about the form values is that there are 10
> fields that start with different words and end in ???? where ???? is
> 0001 - 9999.

Are you talking about HTML/HTTP/Web forms by any chance? You are using the 
CGI module, aren't you?

> I also know how many groups of these values there are so
> I have to do a for loop to read in the values for each group based on
> the starting word and a counter, checking that some of the groups
> aren't empty etc.

If yes, then  @names = $query->param will return all the parameter names and 
you can then use $value = $query->param('foo') to get
each individual value. No need to awkwardly read in your own copies.

jue 




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

Date: Sun, 26 Aug 2007 19:48:50 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Why can't I access an Array like this?
Message-Id: <t3f3d3145p67gs2a0r35ec3ap1pj1ud3m7@4ax.com>

On Sun, 26 Aug 2007 08:39:07 -0700, Bill H <bill@ts1000.us> wrote:

>$in{'TEST0001'} = "Hello 0001";
>$in{'TEST0002'} = "Hello 0002";
>$in{'TEST0003'} = "Hello 0003";
>$in{'TEST0004'} = "Hello 0004";
>
>
>for($i = 1;$i < 5;$i++)
>{
>    $a = substr("0000".$i,-4);

Why this substr() madness? What's wrong with sprintf()?

>    print "TEST$a = $in{'TEST$a'}\n";
>}
>
>for($i = 1;$i < 5;$i++)
>{
>    $a = substr("0000".$i,-4);
>    $b = "TEST$a";
>    print "$b = $in{$b}\n";
>}

How 'bout

  $in{"$TEST$_"} = "$Hello $_" for '0001'..'0004';  # ?


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Sun, 26 Aug 2007 19:51:24 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Why can't I access an Array like this?
Message-Id: <taf3d39o0cnst20hh86pc0kgkho8vpjj6u@4ax.com>

On Sun, 26 Aug 2007 09:34:28 -0700, Ron Bergin <rkb@i.frys.com> wrote:

>Since you're working with a hash, why not simply iterate over it with
>"each"?
>
>foreach my $key ( sort keys %in ) {
>    print "$key = $in{$key}\n";
>}

Just a minor nitpick: you're *not* "iterating over it with C<each>",
but iterating overt its *keys* with C<foreach>.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 797
**************************************


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