Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Does split modify $_ ?

Reply
Thread Tools

Does split modify $_ ?

 
 
rebound@runbox.com
Guest
Posts: n/a
 
      05-19-2005
Hi there,

Please see this code:

use strict;
use warnings;

open(IN, 'myfile.txt') or die "Open failed..$!\n";
while(my @arr = split /:/, <IN>)
{
#print "* $_ *\n";
if (/^#/) { print "Comment found\n"; }
}
close(IN);

I attempted to read the filename (line-by-line), split it using ':' as
seperator and stored the contents in an array. My IF check fails
despite the file having only perl comments.
$cat myfile.txt
#Simple comment
#another comment
##end

I think that $_ is being set to undef by split.
The split documentation does not say that $_ is modified by split.

Can someone please explain why $_ is being modified here.

TIA.
Kasp

 
Reply With Quote
 
 
 
 
Arndt Jonasson
Guest
Posts: n/a
 
      05-19-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>
> use strict;
> use warnings;
>
> open(IN, 'myfile.txt') or die "Open failed..$!\n";
> while(my @arr = split /:/, <IN>)
> {
> #print "* $_ *\n";
> if (/^#/) { print "Comment found\n"; }
> }
> close(IN);
>
> I attempted to read the filename (line-by-line), split it using ':' as
> seperator and stored the contents in an array. My IF check fails
> despite the file having only perl comments.
> $cat myfile.txt
> #Simple comment
> #another comment
> ##end
>
> I think that $_ is being set to undef by split.
> The split documentation does not say that $_ is modified by split.
>
> Can someone please explain why $_ is being modified here.


$_ is not being modified by 'split'; it's in fact never set at all.
You 'while' statement does not both iterate over the lines in the file
and over the items in the split list; it only does the former. You need
another iteration level in your code:

while(my @arr = split /:/, <IN>) {
for (@arr) {
#print "* $_ *\n";
if (/^#/) { print "Comment found\n"; }
}
}

(There may be more to say about this code - I'm new myself - but this
works.)

It's the file you want to read, by the way, not the filename.

I see that I'm assuming that your code was nearly correct already, but
do you want '#' to start a comment within each ':'-delimited field, or
only at the beginning of a line? The latter is more usual. If only at
the beginning of a line, it doesn't make sense to check for that while
iterating over the result of 'split'. I leave the necessary changes as
an exercise.
 
Reply With Quote
 
 
 
 
Kasp
Guest
Posts: n/a
 
      05-19-2005
The <IN> in while loop will modify the $_ to contain the line read.

You can try this:
<IN>
print $_; #or simply print;

I don't understand why split sets $_ to undef...and how can I avoid
doing so?

Cheers
Kasp

--Posted via Google newsgroup...Sorry, folks.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-19-2005
Kasp <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> The <IN> in while loop will modify the $_ to contain the line read.
>
> You can try this:
> <IN>
> print $_; #or simply print;


Did *you* try this?

A lone <$IN> does *not* set $_ to anything. You are working on wrong
assumptions. Read up on "while" in perlsyn to see when it does what
to $_.

> I don't understand why split sets $_ to undef...and how can I avoid
> doing so?


Split has nothing to do with it, $_ has never been set.

Anno
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-20-2005
Kasp wrote:
> The <IN> in while loop will modify the $_ to contain the line read.


For a simple while() conditional, yes.
For a nontrivial while() conditional, no.

while(<IN>){} is the same as while(defined($_=<IN>)){};
while(something <IN>){} is not the same; $_ is *NOT* set.

> You can try this:
> <IN>
> print $_; #or simply print;


Oh, really? Did you actually try it?
$_ = 'original unchanged value';
<IN>;
print $_;
will not put a line of input into $_.

> I don't understand why split sets $_ to undef...and how can I avoid
> doing so?


If you never put anything into $_, then of course it remains undef.
You avoid this by writing code that actually puts data into $_.

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM
Why does split operate over multiple lines in the absence of "ms" ? And why doesn't $_ work with split? Sara Perl Misc 6 04-12-2004 09:07 AM



Advertisments