[32104] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3368 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 27 21:09:35 2011

Date: Wed, 27 Apr 2011 18:09:10 -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           Wed, 27 Apr 2011     Volume: 11 Number: 3368

Today's topics:
    Re: A regex to search for numeric ranges... <kst-u@mib.org>
    Re: A regex to search for numeric ranges... <uri@StemSystems.com>
    Re: A regex to search for numeric ranges... <jimsgibson@gmail.com>
    Re: FAQ flood MUST end <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: FAQ flood MUST end <tadmc@seesig.invalid>
    Re: FAQ flood MUST end <tadmc@seesig.invalid>
    Re: FAQ flood MUST end <jondk@FAKE.EMAIL.net>
    Re: FAQ flood MUST end <uri@StemSystems.com>
    Re: How to AUTOMATICALLY configure CPAN? <ignoramus14859@NOSPAM.14859.invalid>
        Read a hash <dnlchen@gmail.com>
    Re: Read a hash <tadmc@seesig.invalid>
    Re: Read a hash <jimsgibson@gmail.com>
    Re: Read a hash <dnlchen@gmail.com>
    Re: Read a hash <dnlchen@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 27 Apr 2011 13:25:19 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: A regex to search for numeric ranges...
Message-Id: <lnaafbxx00.fsf@nuthaus.mib.org>

Eli the Bearded <*@eli.users.panix.com> writes:
> In comp.lang.perl.misc, Mr P  <misterperl@gmail.com> wrote:
>> I read up on this on the www and I found ideas like
>> 
>> if ( /\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b/ ) ...
>> 
>> which is pretty uncipherable at a glance and just in general not
>> elegant in any sense.
>
> True. That's why it's much better to not use regexps for numerical
> ranges.
>
>> I generally do something like
>> 
>>  if ( /(\d+)/ && $1 > 256 && $1 < 1024 )
>
> I'd write that as
>
>    if ( /(\d+)/ && ($1 > 256) && ($1 < 1024) )
>
> because I like to make sure things operate in the order I want them
> to.

Really?

First off, I hope you're aware that both forms are exactly
equivalent., since "<" binds more tightly than "&&", and "&&"
imposes a left-to-right evaluation with or without the parentheses.

An argument for using the extra parentheses would be that they make
it clearer.  They don't for me personally; in this particular case,
the precedence is carved deeply enough into my brain that it's clear
enough without the parentheses.  But YMMV.  Obviously, different
people have different levels of comfort with the precedence levels
of the various operators.

But I'd write it as:

    if (/(\d+)/ and $1 > 256 and $1 < 1024)

I usually prefer "and" and "or" over "&&" and "||".  On the other
hand, I have been bitten a few times by the *low* precedence of
"and" and "or"; I've mistakenly written things like

    return $this and $that;

which never evaluates $that.

(And none of these are equivalent to the original regexp, which
checks for values from 0 to 255.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Wed, 27 Apr 2011 16:36:27 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: A regex to search for numeric ranges...
Message-Id: <87pqo7xwhg.fsf@quad.sysarch.com>

>>>>> "KT" == Keith Thompson <kst-u@mib.org> writes:

  KT> Eli the Bearded <*@eli.users.panix.com> writes:
  >> I'd write that as
  >> 
  >> if ( /(\d+)/ && ($1 > 256) && ($1 < 1024) )
  >> 
  >> because I like to make sure things operate in the order I want them
  >> to.

  KT> First off, I hope you're aware that both forms are exactly
  KT> equivalent., since "<" binds more tightly than "&&", and "&&"
  KT> imposes a left-to-right evaluation with or without the parentheses.

  KT> An argument for using the extra parentheses would be that they make
  KT> it clearer.  They don't for me personally; in this particular case,
  KT> the precedence is carved deeply enough into my brain that it's clear
  KT> enough without the parentheses.  But YMMV.  Obviously, different
  KT> people have different levels of comfort with the precedence levels
  KT> of the various operators.

i agree with the dropping of unneeded parens. one place i do use extra
parens is with ?:. i find parens around the conditional part helps given
the usually longer total expression. it highlights that as the
conditional part. not critical but a little style thing i do. and it is
especially helpful when doing nested ?: ops.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Wed, 27 Apr 2011 17:20:48 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: A regex to search for numeric ranges...
Message-Id: <270420111720481265%jimsgibson@gmail.com>

In article <lnaafbxx00.fsf@nuthaus.mib.org>, Keith Thompson
<kst-u@mib.org> wrote:

> Eli the Bearded <*@eli.users.panix.com> writes:
> > In comp.lang.perl.misc, Mr P  <misterperl@gmail.com> wrote:
> >> I read up on this on the www and I found ideas like
> >> 
> >> if ( /\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b/ ) ...
> >> 
> >> which is pretty uncipherable at a glance and just in general not
> >> elegant in any sense.
> >
> > True. That's why it's much better to not use regexps for numerical
> > ranges.
> >
> >> I generally do something like
> >> 
> >>  if ( /(\d+)/ && $1 > 256 && $1 < 1024 )
> >
> > I'd write that as
> >
> >    if ( /(\d+)/ && ($1 > 256) && ($1 < 1024) )
> >
> > because I like to make sure things operate in the order I want them
> > to.
> 
> Really?
> 
> First off, I hope you're aware that both forms are exactly
> equivalent., since "<" binds more tightly than "&&", and "&&"
> imposes a left-to-right evaluation with or without the parentheses.
> 
> An argument for using the extra parentheses would be that they make
> it clearer.  They don't for me personally; in this particular case,
> the precedence is carved deeply enough into my brain that it's clear
> enough without the parentheses.  But YMMV.  Obviously, different
> people have different levels of comfort with the precedence levels
> of the various operators.

Another argument for using the extra, redundant parentheses is that it
will work without regard to precedence. I always use the parentheses.
That way I don't have to remember what the operator precedence is and
can worry about other things.

To quote Sherlock Holmes:

"You see," he explained, "I consider that a man's brain originally is
like a little empty attic, and you have to stock it with such furniture
as you choose. A fool takes in all the lumber of every sort that he
comes across, so that the knowledge which might be useful to him gets
crowded out, or at best is jumbled up with a lot of other things so
that he has a difficulty in laying his hands upon it. Now the skilful
workman is very careful indeed as to what he takes into his
brain-attic. He will have nothing but the tools which may help him in
doing his work, but of these he has a large assortment, and all in the
most perfect order. It is a mistake to think that that little room has
elastic walls and can distend to any extent. Depend upon it there comes
a time when for every addition of knowledge you forget something that
you knew before. It is of the highest importance, therefore, not to
have useless facts elbowing out the useful ones."

-- /A Study in Scarlet/, A. C. Doyle.

-- 
Jim Gibson


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

Date: Wed, 27 Apr 2011 13:56:49 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: FAQ flood MUST end
Message-Id: <hhcm88xml3.ln2@goaway.wombat.san-francisco.ca.us>

On 2011-04-27, Uri Guttman <uri@StemSystems.com> wrote:
>>>>>> "JDK" == Jon Du Kim <jondk@FAKE.EMAIL.net> writes:
>
> and you need to learn the difference between an FAQ and posting
> guidelines. try some taking reading comprehension classes.

'Jon Du Kim' appears to be another alias of the 'Ralph Malph' troll.

--keith

-- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information



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

Date: Wed, 27 Apr 2011 16:52:47 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: FAQ flood MUST end
Message-Id: <slrnirh3on.u0t.tadmc@tadbox.sbcglobal.net>

Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> wrote:
> On 2011-04-27, Uri Guttman <uri@StemSystems.com> wrote:
>>>>>>> "JDK" == Jon Du Kim <jondk@FAKE.EMAIL.net> writes:
>>
>> and you need to learn the difference between an FAQ and posting
>> guidelines. try some taking reading comprehension classes.
>
> 'Jon Du Kim' appears to be another alias of the 'Ralph Malph' troll.


I thought that was rather obvious.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 27 Apr 2011 16:56:37 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: FAQ flood MUST end
Message-Id: <slrnirh3vu.u0t.tadmc@tadbox.sbcglobal.net>

Jon Du Kim <jondk@FAKE.EMAIL.net> wrote:
>> I would hope that FAQ answers get reviewed/updated more frequently
>> than every 25 years!
> oh horseshit! That retarded "posting faq" you spam the group with


I do not post any FAQ.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 27 Apr 2011 18:11:01 -0400
From: Jon Du Kim <jondk@FAKE.EMAIL.net>
Subject: Re: FAQ flood MUST end
Message-Id: <f3acd$4db89475$ce534406$7072@news.eurofeeds.com>

On 4/27/2011 5:56 PM, Tad McClellan wrote:
> Jon Du Kim<jondk@FAKE.EMAIL.net>  wrote:
>>> I would hope that FAQ answers get reviewed/updated more frequently
>>> than every 25 years!
>> oh horseshit! That retarded "posting faq" you spam the group with
>
>
> I do not post any FAQ.
Call it whatever you want. How about being open to the idea that it is 
useless in its current form and needs to be completely rewritten? Simply 
put, it reflects a very dated 90's attitude.
usenet is a community of users, not a community of rules. whinging about 
top posting is
ridiculous. The world has moved on...



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

Date: Wed, 27 Apr 2011 18:19:39 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: FAQ flood MUST end
Message-Id: <87zknbwd50.fsf@quad.sysarch.com>

>>>>> "KK" == Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> writes:

  KK> On 2011-04-27, Uri Guttman <uri@StemSystems.com> wrote:
  >>>>>>> "JDK" == Jon Du Kim <jondk@FAKE.EMAIL.net> writes:
  >> 
  >> and you need to learn the difference between an FAQ and posting
  >> guidelines. try some taking reading comprehension classes.

  KK> 'Jon Du Kim' appears to be another alias of the 'Ralph Malph' troll.

that explains the rectum occupied by the cranium syndrome.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Wed, 27 Apr 2011 15:38:50 -0500
From: Ignoramus14859 <ignoramus14859@NOSPAM.14859.invalid>
Subject: Re: How to AUTOMATICALLY configure CPAN?
Message-Id: <VqudnZH4IIlH4yXQnZ2dnUVZ_tidnZ2d@giganews.com>

I learned that if I copy my version of CPAN/Config.pm to
~/.cpan/CPAN/MyConfig.pm prior to invoking cpan, everything seems to
work well.

i


On 2011-04-27, Ignoramus14859 <ignoramus14859@NOSPAM.14859.invalid> wrote:
> I am trying to create a fully scripted (inside a makefile) install of
> perl-5.10.1. 
>

 ...


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

Date: Wed, 27 Apr 2011 16:28:47 -0700 (PDT)
From: DanielC <dnlchen@gmail.com>
Subject: Read a hash
Message-Id: <022eb4e5-074c-46e1-b8f7-3d872ab353c9@h36g2000pro.googlegroups.com>

INPUT (tt):

INFO - {
          'Result' => 'failure',
          'Id' => '1564',
          'dataRecv' => 'adfasdfi',
          'dataSent' => 'rqerqerer',
          'elapsedTime' => '7.023359',
          'externalId' => 'AI2JL4PBLG',
          'responseCode' => '0',
          'responseMsg' => 'n/a',
          'sentAt' => '2011-04-27 00:08:02',
          'serviceId' => '13',
          'serviceName' => 'abc',
          'timeInQueue' => 1
        }

INFO - {
          'Result' => 'failure',
          'Id' => '1565',
          'dataRecv' => 'asdfghjkl',
          'dataSent' => 'qwerrtyu',
          'elapsedTime' => '1.106637',
          'externalId' => 'AI2JL4X1EN',
          'responseCode' => '410',
          'responseMsg' => 'Customer account not active',
          'sentAt' => '2011-04-27 00:14:02',
          'serviceId' => '13',
          'serviceName' => 'def',
          'timeInQueue' => 1
        }


SCRIPT (test.pl):
#!/usr/bin/perl -w
#

my %hash;
local $/ = "\n\n";
while (my $line = <>)
{
    $line =~ s/INFO - {/(/;
    $line =~ s/}/)/;
    %hash = $line;
    print "$hash{'sentAt'}\n";
}

I know how to read the input line by line, then put them into a hash.
Here I just want to try read one section of data into a hash. However
it failed with below errors. Can someone shed some light on this
script and make it work?

$ cat tt|perl test.pl
Odd number of elements in hash assignment at test.pl line 10, <> chunk
1.
Use of uninitialized value in print at test.pl line 11, <> chunk 1.
Odd number of elements in hash assignment at test.pl line 10, <> chunk
2.
Use of uninitialized value in print at test.pl line 11, <> chunk 2.


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

Date: Wed, 27 Apr 2011 19:01:42 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Read a hash
Message-Id: <slrnirhbae.u95.tadmc@tadbox.sbcglobal.net>

DanielC <dnlchen@gmail.com> wrote:
> INPUT (tt):
>
> INFO - {
>           'Result' => 'failure',
>           'Id' => '1564',
>           'dataRecv' => 'adfasdfi',
>           'dataSent' => 'rqerqerer',
>           'elapsedTime' => '7.023359',
>           'externalId' => 'AI2JL4PBLG',
>           'responseCode' => '0',
>           'responseMsg' => 'n/a',
>           'sentAt' => '2011-04-27 00:08:02',
>           'serviceId' => '13',
>           'serviceName' => 'abc',
>           'timeInQueue' => 1
>         }
>
> INFO - {
>           'Result' => 'failure',
>           'Id' => '1565',
>           'dataRecv' => 'asdfghjkl',
>           'dataSent' => 'qwerrtyu',
>           'elapsedTime' => '1.106637',
>           'externalId' => 'AI2JL4X1EN',
>           'responseCode' => '410',
>           'responseMsg' => 'Customer account not active',
>           'sentAt' => '2011-04-27 00:14:02',
>           'serviceId' => '13',
>           'serviceName' => 'def',
>           'timeInQueue' => 1
>         }
>
>
> SCRIPT (test.pl):
> #!/usr/bin/perl -w
> #
>
> my %hash;
> local $/ = "\n\n";
> while (my $line = <>)
> {
>     $line =~ s/INFO - {/(/;
>     $line =~ s/}/)/;
>     %hash = $line;


    %hash = eval $line;  # Danger Will Robinson!


>     print "$hash{'sentAt'}\n";
> }
>
> I know how to read the input line by line, then put them into a hash.
> Here I just want to try read one section of data into a hash. However
> it failed with below errors. Can someone shed some light on this
> script and make it work?
>
> $ cat tt|perl test.pl
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 1.
> Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 2.
> Use of uninitialized value in print at test.pl line 11, <> chunk 2.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 27 Apr 2011 17:07:55 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Read a hash
Message-Id: <270420111707554920%jimsgibson@gmail.com>

In article
<022eb4e5-074c-46e1-b8f7-3d872ab353c9@h36g2000pro.googlegroups.com>,
DanielC <dnlchen@gmail.com> wrote:

> INPUT (tt):
> 
> INFO - {
>           'Result' => 'failure',
>           'Id' => '1564',
>           'dataRecv' => 'adfasdfi',
>           'dataSent' => 'rqerqerer',
>           'elapsedTime' => '7.023359',
>           'externalId' => 'AI2JL4PBLG',
>           'responseCode' => '0',
>           'responseMsg' => 'n/a',
>           'sentAt' => '2011-04-27 00:08:02',
>           'serviceId' => '13',
>           'serviceName' => 'abc',
>           'timeInQueue' => 1
>         }
> 
> INFO - {
>           'Result' => 'failure',
>           'Id' => '1565',
>           'dataRecv' => 'asdfghjkl',
>           'dataSent' => 'qwerrtyu',
>           'elapsedTime' => '1.106637',
>           'externalId' => 'AI2JL4X1EN',
>           'responseCode' => '410',
>           'responseMsg' => 'Customer account not active',
>           'sentAt' => '2011-04-27 00:14:02',
>           'serviceId' => '13',
>           'serviceName' => 'def',
>           'timeInQueue' => 1
>         }
> 
> 
> SCRIPT (test.pl):
> #!/usr/bin/perl -w
> #
> 
> my %hash;
> local $/ = "\n\n";
> while (my $line = <>)
> {
>     $line =~ s/INFO - {/(/;
>     $line =~ s/}/)/;
>     %hash = $line;
>     print "$hash{'sentAt'}\n";
> }
> 
> I know how to read the input line by line, then put them into a hash.
> Here I just want to try read one section of data into a hash. However
> it failed with below errors. Can someone shed some light on this
> script and make it work?

The variable $line is _data_. You are hoping that Perl will treat it as
_code_, but it does not. So your %hash is assigned one key ($line) and
no values. Hence the error about an odd number of elements. When the
value for 'sentAt' is fetched, it is null because it does not exist in
the hash, hence the 'uninitialized value in print' error.

You could try running eval on your data to treat it as code, but that
is not recommended.

Your only hope is to come up with a regular expression that can extract
keys and values from your string, capture them into an array or list,
and assign the array/list to your hash. Something like:

my %hash = ( $line =~ /'[^']*'/g );

That probably doesn't work, and I don't have time to test and fix it,
but somebody smarter than me will no doubt produce a working version
forthwith.

> 
> $ cat tt|perl test.pl
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 1.
> Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 2.
> Use of uninitialized value in print at test.pl line 11, <> chunk 2.

-- 
Jim Gibson


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

Date: Wed, 27 Apr 2011 17:47:07 -0700 (PDT)
From: DanielC <dnlchen@gmail.com>
Subject: Re: Read a hash
Message-Id: <9ca38386-b258-4530-861f-ef52395f986f@34g2000pru.googlegroups.com>

On Apr 27, 5:01=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
> DanielC <dnlc...@gmail.com> wrote:
> > INPUT (tt):
>
> > INFO - {
> > =A0 =A0 =A0 =A0 =A0 'Result' =3D> 'failure',
> > =A0 =A0 =A0 =A0 =A0 'Id' =3D> '1564',
> > =A0 =A0 =A0 =A0 =A0 'dataRecv' =3D> 'adfasdfi',
> > =A0 =A0 =A0 =A0 =A0 'dataSent' =3D> 'rqerqerer',
> > =A0 =A0 =A0 =A0 =A0 'elapsedTime' =3D> '7.023359',
> > =A0 =A0 =A0 =A0 =A0 'externalId' =3D> 'AI2JL4PBLG',
> > =A0 =A0 =A0 =A0 =A0 'responseCode' =3D> '0',
> > =A0 =A0 =A0 =A0 =A0 'responseMsg' =3D> 'n/a',
> > =A0 =A0 =A0 =A0 =A0 'sentAt' =3D> '2011-04-27 00:08:02',
> > =A0 =A0 =A0 =A0 =A0 'serviceId' =3D> '13',
> > =A0 =A0 =A0 =A0 =A0 'serviceName' =3D> 'abc',
> > =A0 =A0 =A0 =A0 =A0 'timeInQueue' =3D> 1
> > =A0 =A0 =A0 =A0 }
>
> > INFO - {
> > =A0 =A0 =A0 =A0 =A0 'Result' =3D> 'failure',
> > =A0 =A0 =A0 =A0 =A0 'Id' =3D> '1565',
> > =A0 =A0 =A0 =A0 =A0 'dataRecv' =3D> 'asdfghjkl',
> > =A0 =A0 =A0 =A0 =A0 'dataSent' =3D> 'qwerrtyu',
> > =A0 =A0 =A0 =A0 =A0 'elapsedTime' =3D> '1.106637',
> > =A0 =A0 =A0 =A0 =A0 'externalId' =3D> 'AI2JL4X1EN',
> > =A0 =A0 =A0 =A0 =A0 'responseCode' =3D> '410',
> > =A0 =A0 =A0 =A0 =A0 'responseMsg' =3D> 'Customer account not active',
> > =A0 =A0 =A0 =A0 =A0 'sentAt' =3D> '2011-04-27 00:14:02',
> > =A0 =A0 =A0 =A0 =A0 'serviceId' =3D> '13',
> > =A0 =A0 =A0 =A0 =A0 'serviceName' =3D> 'def',
> > =A0 =A0 =A0 =A0 =A0 'timeInQueue' =3D> 1
> > =A0 =A0 =A0 =A0 }
>
> > SCRIPT (test.pl):
> > #!/usr/bin/perl -w
> > #
>
> > my %hash;
> > local $/ =3D "\n\n";
> > while (my $line =3D <>)
> > {
> > =A0 =A0 $line =3D~ s/INFO - {/(/;
> > =A0 =A0 $line =3D~ s/}/)/;
> > =A0 =A0 %hash =3D $line;
>
> =A0 =A0 %hash =3D eval $line; =A0# Danger Will Robinson!
>
> > =A0 =A0 print "$hash{'sentAt'}\n";
> > }
>
> > I know how to read the input line by line, then put them into a hash.
> > Here I just want to try read one section of data into a hash. However
> > it failed with below errors. Can someone shed some light on this
> > script and make it work?
>
> > $ cat tt|perl test.pl
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 1.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 2.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 2.
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
> The above message is a Usenet post.
> I don't recall having given anyone permission to use it on a Web site.

Yes, it works. I tried this before but it didn't get the right output.
Why?

%hash =3D eval { $line };


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

Date: Wed, 27 Apr 2011 17:55:11 -0700 (PDT)
From: DanielC <dnlchen@gmail.com>
Subject: Re: Read a hash
Message-Id: <f455fdf0-0463-4bf4-a013-57958d84ce45@22g2000prx.googlegroups.com>

On Apr 27, 5:07=A0pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <022eb4e5-074c-46e1-b8f7-3d872ab35...@h36g2000pro.googlegroups.com>,
>
>
>
>
>
>
>
>
>
> DanielC <dnlc...@gmail.com> wrote:
> > INPUT (tt):
>
> > INFO - {
> > =A0 =A0 =A0 =A0 =A0 'Result' =3D> 'failure',
> > =A0 =A0 =A0 =A0 =A0 'Id' =3D> '1564',
> > =A0 =A0 =A0 =A0 =A0 'dataRecv' =3D> 'adfasdfi',
> > =A0 =A0 =A0 =A0 =A0 'dataSent' =3D> 'rqerqerer',
> > =A0 =A0 =A0 =A0 =A0 'elapsedTime' =3D> '7.023359',
> > =A0 =A0 =A0 =A0 =A0 'externalId' =3D> 'AI2JL4PBLG',
> > =A0 =A0 =A0 =A0 =A0 'responseCode' =3D> '0',
> > =A0 =A0 =A0 =A0 =A0 'responseMsg' =3D> 'n/a',
> > =A0 =A0 =A0 =A0 =A0 'sentAt' =3D> '2011-04-27 00:08:02',
> > =A0 =A0 =A0 =A0 =A0 'serviceId' =3D> '13',
> > =A0 =A0 =A0 =A0 =A0 'serviceName' =3D> 'abc',
> > =A0 =A0 =A0 =A0 =A0 'timeInQueue' =3D> 1
> > =A0 =A0 =A0 =A0 }
>
> > INFO - {
> > =A0 =A0 =A0 =A0 =A0 'Result' =3D> 'failure',
> > =A0 =A0 =A0 =A0 =A0 'Id' =3D> '1565',
> > =A0 =A0 =A0 =A0 =A0 'dataRecv' =3D> 'asdfghjkl',
> > =A0 =A0 =A0 =A0 =A0 'dataSent' =3D> 'qwerrtyu',
> > =A0 =A0 =A0 =A0 =A0 'elapsedTime' =3D> '1.106637',
> > =A0 =A0 =A0 =A0 =A0 'externalId' =3D> 'AI2JL4X1EN',
> > =A0 =A0 =A0 =A0 =A0 'responseCode' =3D> '410',
> > =A0 =A0 =A0 =A0 =A0 'responseMsg' =3D> 'Customer account not active',
> > =A0 =A0 =A0 =A0 =A0 'sentAt' =3D> '2011-04-27 00:14:02',
> > =A0 =A0 =A0 =A0 =A0 'serviceId' =3D> '13',
> > =A0 =A0 =A0 =A0 =A0 'serviceName' =3D> 'def',
> > =A0 =A0 =A0 =A0 =A0 'timeInQueue' =3D> 1
> > =A0 =A0 =A0 =A0 }
>
> > SCRIPT (test.pl):
> > #!/usr/bin/perl -w
> > #
>
> > my %hash;
> > local $/ =3D "\n\n";
> > while (my $line =3D <>)
> > {
> > =A0 =A0 $line =3D~ s/INFO - {/(/;
> > =A0 =A0 $line =3D~ s/}/)/;
> > =A0 =A0 %hash =3D $line;
> > =A0 =A0 print "$hash{'sentAt'}\n";
> > }
>
> > I know how to read the input line by line, then put them into a hash.
> > Here I just want to try read one section of data into a hash. However
> > it failed with below errors. Can someone shed some light on this
> > script and make it work?
>
> The variable $line is _data_. You are hoping that Perl will treat it as
> _code_, but it does not. So your %hash is assigned one key ($line) and
> no values. Hence the error about an odd number of elements. When the
> value for 'sentAt' is fetched, it is null because it does not exist in
> the hash, hence the 'uninitialized value in print' error.
>
> You could try running eval on your data to treat it as code, but that
> is not recommended.
>
> Your only hope is to come up with a regular expression that can extract
> keys and values from your string, capture them into an array or list,
> and assign the array/list to your hash. Something like:
>
> my %hash =3D ( $line =3D~ /'[^']*'/g );
>
> That probably doesn't work, and I don't have time to test and fix it,
> but somebody smarter than me will no doubt produce a working version
> forthwith.
>
>
>
> > $ cat tt|perl test.pl
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 1.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 2.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 2.
>
> --
> Jim Gibson

Why I did "%hash =3D $line;" is because I had tested a script. BTW, I
don't understand - my %hash =3D ( $line =3D~ /'[^']*'/g );


#!/usr/bin/perl -w
#

my %hash =3D (
          'Result' =3D> 'failure',
          'Id' =3D> '1565',
          'dataRecv' =3D> 'asdfghjkl',
          'dataSent' =3D> 'qwerrtyu',
          'elapsedTime' =3D> '1.106637',
          'externalId' =3D> 'AI2JL4X1EN',
          'responseCode' =3D> '410',
          'responseMsg' =3D> 'Customer account not active',
          'sentAt' =3D> '2011-04-27 00:14:02',
          'serviceId' =3D> '13',
          'serviceName' =3D> 'def',
          'timeInQueue' =3D> 1
);

print "$hash{sentAt}\n";


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

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 3368
***************************************


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