Click to See Complete Forum and Search --> : Configuring $/ in Perl code -Help
flintstone
02-24-2001, 10:07 PM
Hi all,
The Basic I/O chap of Learning Perl, mentions that <STDIN> keeps giving the next line of input or undef in a scalar context. What it actually clarifies is that the next line of input is defined upto the newline or whatever we have set $/ to.
What does this mean? If it is possible to breakup a sentence into words (by setting $/ to space), where can I do this? Is it possible to make this change local to that program alone? Can someone give me all the dirt on this?
TIA
jemfinch
02-25-2001, 01:56 AM
Yeah, pretty much.
The best way to answer these kinds of questions is to try them out.
Usually when I (used) to muck with $/, I would declare it local, so as not to mess up other stuff.
Jeremy
flintstone
02-25-2001, 11:42 AM
This is a dumb question, but bear with me:
First I created a test file called goat (as in scapegoat :))
It has two lines:
What a wonderful day
Is that so?
<EOF>
I declared $/ as local and set it to " ".
When I scanned the file into an array, reversed the line and printed it, I got this output:
so?
that day
Is wonderful a What
Using only the ultra basic perl, can I output the file in the same format but in reverse?
TIA
YaRness
02-25-2001, 11:50 AM
i can't understand what you are asking (i'm still short a cup of coffee this morning, so maybe it's just me). why don't you show the code for "scanning" the file into an array, show what you did for the above output, and then show what output you want. or else otherwise clarify what it is you want.
flintstone
02-25-2001, 01:41 PM
ok.
I am looking to take a file and reverse the contents. The snag is it has to reverse each line, not just the order of lines.
so this is what I've done:
1. create file called "goat".
put two lines in there:
Its a wonderful day
Is that so?
2. this is the code I wrote to manipulate the file.
#!/usr/bin/perl -w
local $/=" ";
while(<> ){
push(@input, $_);
}
@output=reverse(@input);
print @output;
print "\n";
This is the output I got:
so?
that day (#there was a leading whitespace here)
is wonderful a what.
Now what i was looking for is this output:
so? that is
day wonderful a what
Now my question was without using regexps is it possible to do this?