Hello!

I have a text file which contains repeating entries as follows:

A: A_value B: B_value C: C_value A: A_value B: B_value C: C_value

My objective is to extract the values and store them to variables to later save into an array of hashes.

My current code is as follows:

my @data = readOnly( "Data.txt" ); my $i = 0; my $index = 0; my $a; my $b; my $c; while( $i < @data ) { if( $data[$i] =~ /A:/ ) { ( , $a) = split( /: /, $data[$i++] ); ( , $b) = split( /: /, $data[$i++] ); ( , $c) = split( /: /, $data[$i++] ); if( exists $baseline{$a} ) { $hash[$index++] = { a => $a, b => $b, c => $c, }; } } }

I've tried declaring the variables $a, $b and $c at the time of assignment as opposed to at the start of the sub and I've tried using a $garbage variable to store the first token of each split (although I don't need it).

I've gotten a variety of errors depending on how I set the code up, but ultimately Perl keeps telling me that $a is uninitialized when I use it in the check for existence.

Insight on why I'm having this issue and guidance on how to code this correctly would be GREATLY appreciated.

Thanks!


In reply to Store result of split directly to variable(s) by Perl_Ally

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!
  • 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, details, 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, summary, 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.