Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

more info on split

by NovMonk (Chaplain)
on Mar 23, 2004 at 19:50 UTC ( #339152=perlquestion: print w/replies, xml ) Need Help??

NovMonk has asked for the wisdom of the Perl Monks concerning the following question:

(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 line 14, +<IN> line 2068. Use of uninitialized value in substitution (s///) at line 20, +<IN> line 2069. Use of uninitialized value in concatenation (.) or string at l +ine 23, <IN> line 2070 Use of uninitialized value in concatenation (.) or string at 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.



Replies are listed 'Best First'.
Re: more info on split
by kvale (Monsignor) on Mar 23, 2004 at 19:54 UTC
    That split creates a 4 element list, with indices (0..3):
    $recno = (split (/ /))[1]; ($qno, $vtext) = (split (/ /,$_,4))[1,3];


Re: more info on split
by CountZero (Bishop) on Mar 23, 2004 at 20:10 UTC
    If the list is short, it is easier to use undef to weed out the unneeded parts and then you don't have to think about zero-based or one-based lists/arrays.


    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://339152]
Approved by Limbic~Region
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-09-24 03:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found