Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: General Question on parsing a variable

by Athanasius (Abbot)
on Feb 07, 2013 at 16:55 UTC ( #1017697=note: print w/ replies, xml ) Need Help??


in reply to Re^2: General Question on parsing a variable
in thread General Question on parsing a variable

It seems each record now ends with a blank line, rather than the word “end” as in the OP. And each record is a collection of lines of the form key = value. So we add another loop, and create a hash in which each key is the storeId (as before), but each value is a (reference to a) nested hash of key/value pairs:

#! perl use strict; use warnings; use Data::Dump qw( pp ); my $var = <<END_RECORDS; storeId = 12 phoneNumber = (270) 111-1111 nameString = My Store1 isOutOfStock = true isReplenishable = true isNotAvailable = false availbilityCode = 0 stockStatus = Out of stock distance = 4.41 city = Highland Village stateCode = TX zipCode = 11111 fullStreet = some random street address storeTypeId = 1 isStorePUTEligible = true walmartExpressStore = false canAddToCart = false storeId = 500 phoneNumber = (270) 111-1111 nameString = my store2 isOutOfStock = true isReplenishable = true isNotAvailable = false availbilityCode = 0 stockStatus = In stock distance = 6.82 city = Roanoke stateCode = TX zipCode = 76262 fullStreet = some random street address storeTypeId = 1 isStorePUTEligible = true walmartExpressStore = false canAddToCart = false END_RECORDS my %hash; while ($var =~ /storeId = (\d+)\s+(.*?)\n(?:\n|$)/gs) { my ($key, $val) = ($1, $2); my %h; $h{$1} = $2 while $val =~ /(\S+)\s*=\s*(.*?)\n/g; $hash{$key} = { %h }; } print "storeID = $_\n", pp($hash{$_}), "\n" for grep { exists $hash{$_}->{stockStatus} && $hash{$_}->{stockStatus} =~ /(?:In|Limited) Stock/i +} keys %hash;

Output:

2:42 >perl 523_SoPW.pl storeID = 500 { availbilityCode => 0, city => "Roanoke", distance => 6.82, fullStreet => "some random street address", isNotAvailable => "false", isOutOfStock => "true", isReplenishable => "true", isStorePUTEligible => "true", nameString => "my store2", phoneNumber => "(270) 111-1111", stateCode => "TX", stockStatus => "In stock", storeTypeId => 1, walmartExpressStore => "false", zipCode => 76262, } 2:42 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re^3: General Question on parsing a variable
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (16)
As of 2015-07-29 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls