Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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,


In reply to Re^3: General Question on parsing a variable by Athanasius
in thread General Question on parsing a variable by diamondsandperls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (11)
    As of 2014-04-21 13:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (495 votes), past polls