Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

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

kazak, we're going to need a whole lot more information than this in order to help you. Sometimes in the process of giving out that information, the answers become obvious, so don't be afraid to share.

My first question would be, how do I determine whether a data entry in a file is a key or a value?

Then I would think about how to write a regex that could detect a key, a value, and a key/value pair.

From your example data given, it's not clear why key5 and key 6 are empty, when you put value 5 and value 6 in your example. Shouldn't they go together? If not, how do you determine that a data value goes with a key?

Is the data somehow identifiable?

Is key5 and 6 empty because they appear alone? Is that what makes them empty?

Here's a thought:

Suppose you could detect key or value, would this pseudocodish outer loop help?

my %hashnew; my $current_key = undef; my $current_value = undef; while (<DATA>) { my ($key, $value) = assign_key_value_from_current_line ($_); $current_key = $key if defined $key; $hashnew{$current_key} = $value; }

You'd then have to create the sub assign_key_value_from_current_line such that if only a key appears on the line, the value is set to undef and returned, and if the value appears then undef is stuffed into the first returned value for the key, and the system would use the first key. You may want an additional check in the main loop for the case where there's no key yet, but a bunch of values that would be tossed on the ground. In the case where both appear, the current key is reassigned and the new value likewise is recorded.

If you can't figure out difference between key and value, then it may be impossible, but from what you've given I don't know. Also start small, if you can't get all of the solution all at once, perhaps try a few simple steps, perhaps you can organize your data, or get it "part way" completed. In such a case, you might discover that it's good enough for what you're trying to do.

And as moritz notes, it'd be nice to see what you've tried already.

In reply to Re: Parse a file into a hash by raybies
in thread [Resolved] Parse a file into a hash by kazak

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    marto wanders off
    [usemodperl]: Corion it's really not misguided, it's the only way, to do something... wonderful IMHO
    [Corion]: usemodperl: Why don't you set up your own (http-only) CPAN mirror? Or just fatpack your scripts? I wonder what problem you're trying to solve here.
    [usemodperl]: don't worry about that, it's really cool, i promise!
    [Corion]: usemodperl: Well, if the world changes and makes your "wonderful" approach not work anymore, you can either change your approach, or change the world. You seem to want to change others instead.
    [usemodperl]: no dude i'm just asking how to access meta via http and you are getting defensive for some reason (did you turn off http at meta? :-)
    [choroba]: why do you need to access metacpan via http?
    [usemodperl]: stop asking why, just say how
    usemodperl pulls teeth...
    [usemodperl]: change the world? you mean BREAK THE WEB? we will regret this once google starts pulling certs for cursing or something...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2018-06-24 16:33 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.