[30856] in Perl-Users-Digest
Perl-Users Digest, Issue: 2101 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 6 21:09:51 2009
Date: Tue, 6 Jan 2009 18:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 6 Jan 2009 Volume: 11 Number: 2101
Today's topics:
Re: -1 ** 0 == -1 ??? (Tim McDaniel)
Re: hash table usage questions <uri@stemsystems.com>
Re: Is there any IDE for perl? <george@example.invalid>
Parsing a string into a hash <wibble@fakedomain.com>
Re: Parsing a string into a hash <someone@example.com>
Re: Parsing a string into a hash <josef.moellers@fujitsu-siemens.com>
Re: Parsing a string into a hash <wibble@fakedomain.com>
Re: Parsing a string into a hash sln@netherlands.com
Re: Parsing a string into a hash sln@netherlands.com
Re: Parsing a string into a hash <tadmc@seesig.invalid>
Re: Parsing a string into a hash sln@netherlands.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 6 Jan 2009 16:34:58 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: -1 ** 0 == -1 ???
Message-Id: <gk017h$mni$1@reader1.panix.com>
In article <49632cf2$0$30297$da0feed9@news.zen.co.uk>,
RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:
>I assume that any programming language older than a few months with
>widespread usage is suitable for numeric computation
Um, you might be over-optimistic.
There's the famous feature of PL/I where 25 + 1/3 either throws a
fixed-point overflow error or evaluates to 5.33333... I've seen both
statements and I have no way to test it;
<http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.aix.pli.doc/ibml2d41004828.htm>
says it's an error but also mentions left-truncation.
The dc program was designed to do nothing but be a Reverse Polish
Notation calculator, but by default it does only pure integer
calculation (you can enter numbers with a decimal point: the
fractional part will be discarded silently). You can change that, but
it provides only a fixed number of decimal places.
For that matter, it surprises new programmers when they learn that 1/3
equals 0, or that 1.0/3.0*3.0 does not necessarily equal 1.0, in many
languages.
(I have a dim memory that older Visual Basics were loosey-goosey about
mixing booleans and integers, and that the "not" operator is a bitwise
"not" instead of a logical one, and FALSE is 0, and integers are
ones-complement, so for all integers I except -1, both I and NOT I
were considered true. But I'm not at all 100% certain of that, and
that's not really numeric.)
But that's really quibbling. Your general point is quite true.
It's really REALLY rare for someone to find a new fundamental flaw in
a long-standing language. By far the highest chance is that they're
misunderstanding something. There's a much smaller chance that
they've hit a known long-standing problem.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Tue, 06 Jan 2009 15:07:04 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: hash table usage questions
Message-Id: <x7aba4i4gn.fsf@mail.sysarch.com>
>>>>> "CD" == C DeRykus <ced@blv-sam-01.ca.boeing.com> writes:
CD> On Jan 2, 2:45 pm, Uri Guttman <u...@stemsystems.com> wrote:
>>
>> sure that could be faster but it may not be as you think. you make N
>> calls to delete and calls to each in a loop. perl ops are the major
>> bottleneck in perl speed. if the slice lists are short enough (and they
>> could still be pretty long), IMO my solution should be faster. but only
>> benchmarking can tell for sure. my method does use more ram but ram is
>> cheaper than speed. :)
>>
CD> I don't how meaningful just a raw opcode count is but the hash-slice-
CD> delete does have fewer:
CD> perl -MO=Concise -e 'delete @h{ grep !/\.cc$/, keys %h }'|wc -l
CD> 17
CD> perl -MO=Concise -e ' /.cc$/ and delete $h{$_} while defined ($_=each
CD> %h);'
CD> 25
the raw ops count is nice but doesn't take into consideration the
execution of the while loop vs grep and slicing. the while loop will
execute many more perl ops because it stays in perl most of the
time. the grep/slice stays in perl's core (c) more of the time so it
will likely be faster (at least on normal sized ram usage).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 6 Jan 2009 18:51:43 -0700
From: George <george@example.invalid>
Subject: Re: Is there any IDE for perl?
Message-Id: <mgpnzyt0smsj.rql2ymejwdw4.dlg@40tude.net>
On Fri, 2 Jan 2009 06:49:44 -0600, Tad J McClellan wrote:
> George <george@example.invalid> wrote:
>
>> [1 quoted line suppressed]
>
>
> http://tinyurl.com/9ol5xl
Back when I could afford MS products, that was one of the features I didn't
like.
Isn't there something called strawberry perl with an IDE?
--
George
The true history of my administration will be written 50 years from now,
and you and I will not be around to see it.
George W. Bush
Picture of the Day http://apod.nasa.gov/apod/
------------------------------
Date: Tue, 6 Jan 2009 12:55:18 -0000
From: "IanW" <wibble@fakedomain.com>
Subject: Parsing a string into a hash
Message-Id: <496354c7$0$20412$8d2e0cab@news.newsgroup-binaries.com>
Hi
I have some strings like this (the print_r output from PHP):
my $str = "Array
(
[SY] => 1
[DN] => 1
[YO] => 1
[MK] => 1
[N] => 9
[HP] => 2
[LA] => 1
[NP] => 1
[SW] => 3
)";
I want to be able to parse that string into a Perl hash where the keys are
the letters inside the square brackets (eg: SY or N) and the values are the
numbers after the => (they will always be numeric).
I feel this should be achievable with a cunning one liner using the map
function along with some regexp output (/\[(\w+)\]\D+?(\d+)/) but I don't
know how to begin. ANy help appreciated.
Thanks
Ian
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
------------------------------
Date: Tue, 06 Jan 2009 05:12:24 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: Parsing a string into a hash
Message-Id: <YMI8l.31931$Ac2.19325@newsfe01.iad>
IanW wrote:
>
> I have some strings like this (the print_r output from PHP):
>
> my $str = "Array
> (
> [SY] => 1
> [DN] => 1
> [YO] => 1
> [MK] => 1
> [N] => 9
> [HP] => 2
> [LA] => 1
> [NP] => 1
> [SW] => 3
> )";
>
> I want to be able to parse that string into a Perl hash where the keys are
> the letters inside the square brackets (eg: SY or N) and the values are the
> numbers after the => (they will always be numeric).
>
> I feel this should be achievable with a cunning one liner using the map
> function along with some regexp output (/\[(\w+)\]\D+?(\d+)/) but I don't
> know how to begin. ANy help appreciated.
$ perl -le'
my $str = "Array
(
[SY] => 1
[DN] => 1
[YO] => 1
[MK] => 1
[N] => 9
[HP] => 2
[LA] => 1
[NP] => 1
[SW] => 3
)";
my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
use Data::Dumper;
print Dumper \%hash;
'
$VAR1 = {
'LA' => '1',
'N' => '9',
'NP' => '1',
'SW' => '3',
'YO' => '1',
'SY' => '1',
'DN' => '1',
'HP' => '2',
'MK' => '1'
};
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
------------------------------
Date: Tue, 06 Jan 2009 14:12:59 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Parsing a string into a hash
Message-Id: <gjvlc6$lv6$1@nntp.fujitsu-siemens.com>
IanW wrote:
> Hi
>
> I have some strings like this (the print_r output from PHP):
>
> my $str = "Array
> (
> [SY] => 1
> [DN] => 1
> [YO] => 1
> [MK] => 1
> [N] => 9
> [HP] => 2
> [LA] => 1
> [NP] => 1
> [SW] => 3
> )";
>
> I want to be able to parse that string into a Perl hash where the keys are
> the letters inside the square brackets (eg: SY or N) and the values are the
> numbers after the => (they will always be numeric).
>
> I feel this should be achievable with a cunning one liner using the map
> function along with some regexp output (/\[(\w+)\]\D+?(\d+)/) but I don't
> know how to begin. ANy help appreciated.
First the obvious question: What have you tried so far and where did
your code not meet your expectations?
Why do you want a "cunning one liner"? What's so bad about
my %h;
foreach (split(/\n/, $str)) {
next unless /\[(\w{1,2})\]\s+=>\s+(\d+)/;
$h{$1} = $2;
}
"cunning one liner"s often turn out to be "obfuscated one liners" one
day, that may cost you some time to figure out.
Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
------------------------------
Date: Tue, 6 Jan 2009 14:55:01 -0000
From: "IanW" <wibble@fakedomain.com>
Subject: Re: Parsing a string into a hash
Message-Id: <496370d5$0$20460$8d2e0cab@news.newsgroup-binaries.com>
"John W. Krahn" <someone@example.com> wrote in message
news:YMI8l.31931$Ac2.19325@newsfe01.iad...
> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
Thanks, I didn't know you could do that. How about if one has more than one
string to parse, like:
my $str_A = "Array
(
[SY] => 4
[DN] => 2
[YO] => 22
}
my $str_B = "Array
(
[SY] => 3
[DN] => 1
[YO] => 11
}
.. AND you wanted to build a master hash so that the key is still the
letters within the square brackets but the values go into sub-hashes.
E.g: you would get a dump something like:
$VAR1 = {
SY => {
A => 4,
B => 3
},
DN => {
A => 2,
B => 1
},
YO => {
A => 22,
B => 11
}
};
Can each individual string be parsed into %hash in one line? Perhaps this is
where map comes in?
I've got nothing against using a loop, it's just I would like to know if
it's possible and I might learn something more about parsing strings in the
same way I just learnt that technique you gave for a standard hash.
Regards
Ian
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
------------------------------
Date: Tue, 06 Jan 2009 17:30:28 GMT
From: sln@netherlands.com
Subject: Re: Parsing a string into a hash
Message-Id: <8657m4l9itm3aik0eegfj8tfq1vjo5ik2h@4ax.com>
On Tue, 6 Jan 2009 14:55:01 -0000, "IanW" <wibble@fakedomain.com> wrote:
>"John W. Krahn" <someone@example.com> wrote in message
>news:YMI8l.31931$Ac2.19325@newsfe01.iad...
>
>> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
>
>Thanks, I didn't know you could do that. How about if one has more than one
>string to parse, like:
>
>my $str_A = "Array
>(
> [SY] => 4
> [DN] => 2
> [YO] => 22
>}
>
>my $str_B = "Array
>(
> [SY] => 3
> [DN] => 1
> [YO] => 11
>}
>
>.. AND you wanted to build a master hash so that the key is still the
>letters within the square brackets but the values go into sub-hashes.
>E.g: you would get a dump something like:
>
>$VAR1 = {
> SY => {
> A => 4,
> B => 3
> },
> DN => {
> A => 2,
> B => 1
> },
> YO => {
> A => 22,
> B => 11
> }
>};
>
>Can each individual string be parsed into %hash in one line? Perhaps this is
>where map comes in?
>
No it can't.
sln
--------------------------------------------
use strict;
use warnings;
my $strA = "Array
(
[SY] => 1
[DN] => 1
[YO] => 1
[MK] => 1
[N] => 9
[HP] => 2
[LA] => 1
[NP] => 1
[SW] => 3
)";
my $strB = "Array
(
[SY] => 11
[DN] => 11
[YO] => 11
[MK] => 11
[N] => 91
[HP] => 21
[LA] => 11
[NP] => 11
[SW] => 31
)";
my @Sarray = (\$strA, \$strB);
my %Hash = ();
my $subkey = 'A';
for my $strref (@Sarray)
{
my $href = {$$strref =~ /\[(\w+)\]\D*(\d+)/g};
while (my ($key,$value) = each %{$href}) {
$Hash{$key}->{$subkey} = $value;
}
$subkey++;
}
use Data::Dumper;
print Dumper \%Hash;
__END__
$VAR1 = {
'LA' => {
'A' => '1',
'B' => '11'
},
'SW' => {
'A' => '3',
'B' => '31'
},
'YO' => {
'A' => '1',
'B' => '11'
},
'N' => {
'A' => '9',
'B' => '91'
},
'DN' => {
'A' => '1',
'B' => '11'
},
'SY' => {
'A' => '1',
'B' => '11'
},
'NP' => {
'A' => '1',
'B' => '11'
},
'HP' => {
'A' => '2',
'B' => '21'
},
'MK' => {
'A' => '1',
'B' => '11'
}
};
------------------------------
Date: Tue, 06 Jan 2009 21:02:40 GMT
From: sln@netherlands.com
Subject: Re: Parsing a string into a hash
Message-Id: <sjh7m4hjc9rk74hufk35pf3mdsfla3irrv@4ax.com>
On Tue, 6 Jan 2009 14:55:01 -0000, "IanW" <wibble@fakedomain.com> wrote:
>"John W. Krahn" <someone@example.com> wrote in message
>news:YMI8l.31931$Ac2.19325@newsfe01.iad...
>
>> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
>
>Thanks, I didn't know you could do that. How about if one has more than one
>string to parse, like:
>
>my $str_A = "Array
>(
> [SY] => 4
> [DN] => 2
> [YO] => 22
>}
>
>my $str_B = "Array
>(
> [SY] => 3
> [DN] => 1
> [YO] => 11
>}
>
>.. AND you wanted to build a master hash so that the key is still the
>letters within the square brackets but the values go into sub-hashes.
>E.g: you would get a dump something like:
>
>$VAR1 = {
> SY => {
> A => 4,
> B => 3
> },
> DN => {
> A => 2,
> B => 1
> },
> YO => {
> A => 22,
> B => 11
> }
>};
>
>Can each individual string be parsed into %hash in one line? Perhaps this is
>where map comes in?
>
>I've got nothing against using a loop, it's just I would like to know if
>it's possible and I might learn something more about parsing strings in the
>same way I just learnt that technique you gave for a standard hash.
>
>Regards
>Ian
>
This would work as well if parsing from a file.
sln
-------------------------------------------
use strict;
use warnings;
my $String = join '', <DATA>;
my %Hash = ();
my $subkey = 'A';
while ( $String =~ /Array\s*\(([^)]*)/g )
{
my $str = $1;
my $href = {$str =~ /\[(\w+)\]\D*(\d+)/g};
while (my ($key,$value) = each %{$href}) {
$Hash{$key}{$subkey} = $value;
}
$subkey++;
}
use Data::Dumper;
print Dumper \%Hash;
__DATA__
Array
(
[SY] => 1
[DN] => 1
[YO] => 1
[MK] => 1
[N] => 9
[HP] => 2
[LA] => 1
[NP] => 1
[SW] => 3
)
Array
(
[SY] => 11
[DN] => 11
[YO] => 11
[MK] => 11
[N] => 91
[HP] => 21
[LA] => 11
[NP] => 11
[SW] => 31
)
------------------------------
Date: Tue, 6 Jan 2009 15:13:46 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Parsing a string into a hash
Message-Id: <slrngm7ica.dgu.tadmc@tadmc30.sbcglobal.net>
IanW <wibble@fakedomain.com> wrote:
Do you have permission to use that domain from its Korean owner?
If not, then you should use a more polite form of address munging,
perhaps "wibble@fakedomain.com.invalid" or some such.
> "John W. Krahn" <someone@example.com> wrote in message
^^^^^^^^^^^
Note that the domain that John uses for munging is a special
domain created just for such things.
> news:YMI8l.31931$Ac2.19325@newsfe01.iad...
>
>> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
>
> Thanks, I didn't know you could do that.
perldoc perlop
documents what happens when m// is used in a list context:
m// in list context returns a list consisting of the
subexpressions matched by the parentheses in the pattern
> How about if one has more than one
> string to parse, like:
>
> my $str_A = "Array
> (
> [SY] => 4
> [DN] => 2
> [YO] => 22
> }
Where is the matching double quote character?
Where is the closing parenthesis?
Where is the opening curly brace?
Please post Real Perl Code that you have copy/pasted rather
than attempt to retype it.
Have you seen the Posting Guidelines that are posted here frequently?
> my $str_B = "Array
> (
> [SY] => 3
> [DN] => 1
> [YO] => 11
> }
>
> .. AND you wanted to build a master hash so that the key is still the
> letters within the square brackets but the values go into sub-hashes.
> E.g: you would get a dump something like:
>
> $VAR1 = {
> SY => {
> A => 4,
> B => 3
> },
> DN => {
> A => 2,
> B => 1
> },
> YO => {
> A => 22,
> B => 11
> }
> };
You have chosen an extremely poor data structure for your strings.
We are expected to get the "A" and "B" from the _name_ of a variable?
Down that path lies madness.
I will use a hash with "A" and "B" keys to hold the two strings instead.
> Can each individual string be parsed into %hash in one line?
I don't know, nor do I care.
Obsession with one-liners is unhealthy in production code.
I'll do it with 3 easily-understood lines rather than construct
an infathomable one-liner.
-------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my %strings = (
A => "Array
(
[SY] => 4
[DN] => 2
[YO] => 22
)",
B => "Array
(
[SY] => 3
[DN] => 1
[YO] => 11
)",
);
my %result;
foreach my $str ( keys %strings ) {
while ( $strings{$str} =~ /\[(\w+)] => (\d+)/g ) {
$result{$1}{$str} = $2;
}
}
print Dumper \%result;
-------------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Tue, 06 Jan 2009 22:11:13 GMT
From: sln@netherlands.com
Subject: Re: Parsing a string into a hash
Message-Id: <o0l7m4p89gva7d3cdv4iu152imcla2rlol@4ax.com>
On Tue, 6 Jan 2009 14:55:01 -0000, "IanW" <wibble@fakedomain.com> wrote:
>"John W. Krahn" <someone@example.com> wrote in message
>news:YMI8l.31931$Ac2.19325@newsfe01.iad...
>
>> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
>
>Thanks, I didn't know you could do that. How about if one has more than one
>string to parse, like:
>
>my $str_A = "Array
>(
> [SY] => 4
> [DN] => 2
> [YO] => 22
>}
>
>my $str_B = "Array
>(
> [SY] => 3
> [DN] => 1
> [YO] => 11
>}
>
>.. AND you wanted to build a master hash so that the key is still the
>letters within the square brackets but the values go into sub-hashes.
>E.g: you would get a dump something like:
>
>$VAR1 = {
> SY => {
> A => 4,
> B => 3
> },
> DN => {
> A => 2,
> B => 1
> },
> YO => {
> A => 22,
> B => 11
> }
>};
>
>Can each individual string be parsed into %hash in one line? Perhaps this is
>where map comes in?
>
>I've got nothing against using a loop, it's just I would like to know if
>it's possible and I might learn something more about parsing strings in the
>same way I just learnt that technique you gave for a standard hash.
>
>Regards
>Ian
>
>
Adding one more comment. In reality, anything can be parsed.
The most aggregious things that sticks out in your post is the
attempt to write a pseudo hash structure within a string that
is hardcoded in a Perl script. Maybe to show people your intentions,
I don't know.
You stated it is generated (from php?). The data and the processing code
is that what you mean?
There is no variablity, you might as well construct the hash your
looking for by hand, its %90 there in your statics.
Maybe your just looking for clues in the details. However, your not
rational in your approach. Usually, I draw the line with impractical
premise. But I thought you might be looking for elements in a bigger
picture.
Don't sell the responders here short, clearly state your intentions!
sln
------------------------------
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 2101
***************************************