NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
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:
(the commented out version works great; the lines modeled after Fletch's suggestion above give me the error:#!/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"; } }
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: more info on split
by kvale (Monsignor) on Mar 23, 2004 at 19:54 UTC | |
|
Re: more info on split
by CountZero (Bishop) on Mar 23, 2004 at 20:10 UTC |