(Note: This is related to an earlier thread on using strict.)

Would someone mind commenting a bit more on this fine suggestion? I've been playing with it all afternoon and am just not getting it. fletch wrote:

On an unrelated note, using undef rather than a junk variable is better practice if you don't need one of the return values from split (or wrap the split in parens and subscript it to pull out what you want, e.g. ( $foo, $bar ) = ( split( /:/ ) )[1,4]).

I've consulted the tutorials and super search, and there's not a lot on split (in the tutorials)or way too much to find anything relevant (using supersearch). Maybe if I master this, I'll take care of that lack myself. I'm assuming, incidentally, that the last paren in the above is part of the sentence, not the code, as the parens are unbalanced otherwise. Anyway, here's my script at the moment:

#!/usr/bin/perl use strict; use warnings; open (IN, "infile") or die "Can't open input file: $!\n"; open (OUT, ">outfile") or die "Can't create outfile for write: $!\n "; while (<IN>) { if (/^##recstart/) { #($junk,$recno) = split; $recno = (split (/ /))[2]; $recno =~ s/'//g; } elsif (/^##v/) { #($junk,$qno,$junk2,$vtext) = split (/ /,$_,4); ($qno, $vtext) = (split (/ /,$_,4))[2,4]; $qno =~ s/'//g; $vtext =~ s/'//g; } else { print OUT "$recno^$qno^$vtext"; } }
(the commented out version works great; the lines modeled after Fletch's suggestion above give me the error:

Use of uninitialized value in substitution (s///) at verb.pl line 14, +<IN> line 2068. Use of uninitialized value in substitution (s///) at verb.pl line 20, +<IN> line 2069. Use of uninitialized value in concatenation (.) or string at verb.pl l +ine 23, <IN> line 2070 Use of uninitialized value in concatenation (.) or string at verb.pl l +ine 23, <IN> line 2070

These are only the last couple of lines on a file that scrolls forever.)

My input file is 3 line records out of which I need particular bits from the first 2 lines assigned to variables, and then printed once into a new file. The if/elsif/else logic works fine for that, until I try this clever new way of using split. One thing about the second split-- the 4th element (vtext) needs to pick up everything to the end of the line, which is why I tell split I have only 4 elements.

I'm hoping someone can point me to more examples of the subscripting used with split, but I accept that I may be too much a novice to see the real error of my ways. I am trying though. Either way, thanks for the enlightenment.

Pax,

NovMonk


In reply to more info on split by NovMonk

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.