Bit Bucket?

$

$_

I have a string like this:
-rwx------ 1 user group 0 Jan 06 15:07 a file with spaces

and im placing that string in an array with this command:
my @dline = split(' ', $dirline);

now notice that there are three spaces between 'with spaces'
so when i re-stringify this array using this command:
my $filename = join(' ', @dline);

$filename now contains 'a file with spaces'
notice only one space between 'with spaces'

did split eat my extra spaces? i doubt it would have eaten other chars.
any suggestions or explanations? perhaps another way of doing this?

thanks, and i hope my post hasnt upset you very much, have a nice day :)
 
J

Josef Möllers

$_"@_.% said:
I have a string like this:
-rwx------ 1 user group 0 Jan 06 15:07 a file with spaces

and im placing that string in an array with this command:
my @dline = split(' ', $dirline);

now notice that there are three spaces between 'with spaces'
so when i re-stringify this array using this command:
my $filename = join(' ', @dline);

$filename now contains 'a file with spaces'
notice only one space between 'with spaces'

did split eat my extra spaces? i doubt it would have eaten other chars.
any suggestions or explanations? perhaps another way of doing this?

thanks, and i hope my post hasnt upset you very much, have a nice day :)

split usually splits on a pattern. As a special case, a space (' ') will
split on the pattern /\s+/, to emulate awk's default behaviour.

see perldoc -f split and scroll almost to the end.
 
$

$_

Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
wrote:

split usually splits on a pattern. As a special case, a space (' ') will
split on the pattern /\s+/, to emulate awk's default behaviour.

see perldoc -f split and scroll almost to the end.

Thank you very much that is very helpfull.

What i ended up doing was replacing the spaces with *'s which should work out
since a * is illegal in a filename (i think).

#example raw dir lines
#need to distinguish btwn dirs and files and reformat for display
#desired format is: file size or <DIR> date
#-rwx------ 1 user group 217752 Nov 05 1995 file.exe
#drwx------ 1 user group 0 Jan 07 11:57 dir3
#-rwx------ 1 user group 0 Jan 06 15:07 a file with spaces
#-rwx------ 1 user group 13 Jan 06 15:08 file.txt

$dirline =~ s/\s/*/g;
 
U

Uri Guttman

J> Thank you very much that is very helpfull.

J> What i ended up doing was replacing the spaces with *'s which should work out
J> since a * is illegal in a filename (i think).

no it isn't.

on unix flavors, the only char not allowed in a filename is /

instead of split, use a regex so you get the fields you want. and there
is at least one module on cpan that parses out ls -l listings.

uri
 
$

$_

J" == said:
J> Thank you very much that is very helpfull.

J> What i ended up doing was replacing the spaces with *'s which should work out
J> since a * is illegal in a filename (i think).

no it isn't.

on unix flavors, the only char not allowed in a filename is /

instead of split, use a regex so you get the fields you want. and there
is at least one module on cpan that parses out ls -l listings.

uri
Thanks! yea this is soo difficult, some ftp site use an arbitrary number of spaces
in the time format.
 
B

Ben Morrow

Uri Guttman said:
J> What i ended up doing was replacing the spaces with *'s which
J> should work out since a * is illegal in a filename (i think).

no it isn't.

on unix flavors, the only char not allowed in a filename is /

Just for correctness: and "\000", ASCII NUL.

Ben
 
W

Walter Roberson

:> on unix flavors, the only char not allowed in a filename is /

:Just for correctness: and "\000", ASCII NUL.

To be even more pedantic: a pathname that starts with exactly two
slashes may be interpreted in an implimentation-defined manner.
In theory, that implimentation-defined manner could treat later slashes
as filename components (or as anything else it wanted). [IEEE 1003.1
section 2.2.2.57] Just not as POSIX standard filenames, which -are-
defined to not contain slash or null [2.2.2.32].

To avoid confusion here: POSIX.1 is distinguishing between "pathnames"
and "filenames", and to POSIX.1, a "filename" is the part in-between
slashes, so it is perfectly correct to say that POSIX.1 "filenames"
cannot contain / or null -- but POSIX.1 leaves an loophole for
local non-POSIX structures in which slash might be just another
character.
 
T

Tore Aursand

I have a string like this:
-rwx------ 1 user group 0 Jan 06 15:07 a file with spaces

and im placing that string in an array with this command:
my @dline = split(' ', $dirline);

now notice that there are three spaces between 'with spaces'
so when i re-stringify this array using this command:
my $filename = join(' ', @dline);

$filename now contains 'a file with spaces'
notice only one space between 'with spaces'

split() normally takes a pattern as first argument; If you use ' ' as
argument, Perl will read this as '\s+', ie. matching one or more spaces.

You should use '/\s/' as pattern, ie.:

my @dline = split(/\s/, $dirline);
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top