Trailing whitespace question

N

nun

I'm reading data from a text file into an array like this:

push @AoA, [(
substr($_, 0, 4),
substr($_, 4, 20),
substr($_, 24, 30),
substr($_, 54, 8),
substr($_, 70, 8),
substr($_, 103, 20),
substr($_, 123, 1),
)];


I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.

I see from Google that:

s/ *$//;

should do what I want, but I'm unsure how to modify my script to include
this. Can someone help?


DB
 
D

Dr.Ruud

nun schreef:

That should be

s/ +$//;

or even

s/\s+$//;

substr($_, 0, 4),
substr($_, 4, 20),
substr($_, 24, 30),
substr($_, 54, 8),
substr($_, 70, 8),
substr($_, 103, 20),
substr($_, 123, 1),

Or

m/^(.{4})(.{20})(.{30})(.{8)).{8}(.{8)).{25}(.{20})(.)/s
 
T

Tad McClellan

nun said:
I'm reading data from a text file into an array like this:

push @AoA, [(
substr($_, 0, 4),
substr($_, 4, 20),
substr($_, 24, 30),
substr($_, 54, 8),
substr($_, 70, 8),
substr($_, 103, 20),
substr($_, 123, 1),
)];


I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.

I see from Google that:

s/ *$//;


Removing zero spaces is rather silly.

There is no point unless there is at least one space.

should do what I want, but I'm unsure how to modify my script to include
this. Can someone help?


$AoA[-1][2] =~ s/ +$//;
 
D

Dr.Ruud

Michele Dondi schreef:
Michele Dondi:

Or
unpack 'A4A20A30A8x8A8x25A20A1' => $_;
(And one would probably include some whitespace to improve
readability.)

Are you sure "A" is powerful enough? In de documentation I see that only
ASCII is captured.
(perl 5.8.1, perldoc -f pack)
 
A

Ala Qumsieh

Michele said:
I'm reading data from a text file into an array like this:

push @AoA, [(

Why @AoA? It's just a plain array of strings...

No. It's an array of array references. @AoA is appropriate.
substr($_, 0, 4),
substr($_, 4, 20),
substr($_, 24, 30),
substr($_, 54, 8),
substr($_, 70, 8),
substr($_, 103, 20),
substr($_, 123, 1),
)];


I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.

You want to know about =~.
I see from Google that:

s/ *$//;

If you want to be picky, s/ *\z// would be more precise. But what you
wrote may be what you want. If there are no newlines involved, they
will be the same.

That is not correct. The OP wants to make sure that whatever is returned by

substr($_, 24, 30)

does not contain trailing spaces.

--Ala
 
D

Dr.Ruud

Dr.Ruud schreef:
Michele Dondi:

Are you sure "A" is powerful enough? In de documentation I see
that only ASCII is captured.
(perl 5.8.1, perldoc -f pack)

Test on perl 5.8.6:

perl -wle'
$_ = "\x{20ac}"; # euro sign
print length unpack "A*", $_
'
3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top