Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Your site has saved me hours of work that I cannot begin to express my satisfaction..."

Geography

Where in the world do Tek-Tips members come from?

Several Problems working with stringsHelpful Member!(2) 

TSch (TechnicalUser)
13 Jul 12 8:39
Hi everyone,

could you help me out again ?

Let's say I got the following strings stored in a file input.txt :

CODE

Value1 Value2   Value3     Value4
Value5  Value6     Value7  Value8 

First I tried to read all the values into an array:

CODE

open (IN1, "input.txt");
@values = <IN1>;
foreach (@values)
{
print "Here is a single value: ", $_, "\n":
} 

But this gave me only 2 values:

CODE

Here is a single value: Value1 Value2   Value3     Value4
Here is a single value: Value5  Value6     Value7  Value8 

Next I changed it to scalar reading:

CODE

open (IN1, "input.txt");
while (<IN1>)
{
print "Here is a single value: ", $_, "\n":
} 

Same output ...

CODE

Here is a single value: Value1 Value2   Value3     Value4
Here is a single value: Value5  Value6     Value7  Value8 

How can I solve this problem ?
What if the value separator is not a blank but something else (e.G. a ; or a -) ?

Best Regards,
Thomas
Helpful Member!  feherke (Programmer)
13 Jul 12 9:08
Hi

What is your goal ? Something like this ?

CODE --> sample-output

Here is a single value: Value1
Here is a single value: Value2
...
Here is a single value: Value8 

That will not be so easy in Perl as it looks. Perl supports only fixed strings as record separator, so you can not just split the record on any whitespace characters :

Quote (man perlvar)

Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)

Personally I would slurp in the file in one piece then split it "manually" :

CODE --> Perl

open IN1, "input.txt";
undef $/;

foreach (split /\s+/, <IN1>) {
  print "Here is a single value: ", $_, "\n";
} 

Feherke.
http://feherke.github.com/

Helpful Member!  prex1 (Programmer)
15 Jul 12 17:28
feherke, split without parameters splits the $_ variable on multiple spaces. So this should work:

CODE

while(<IN1>){
  push @values,split;
}
for(@values){
  print "Here is a single value: ", $_, "\n":
} 

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

feherke (Programmer)
16 Jul 12 3:20
Hi

Quote (Franco)

feherke, split without parameters splits the $_ variable on multiple spaces.
Ah, right. I done too many split()ing in Java recently...

Feherke.
http://feherke.github.com/

TSch (TechnicalUser)
26 Jul 12 5:04
Thanks a lot for the help everyone !

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close