Home > Archive > PERL Beginners > January 2007 > Why won't split() find \n?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Why won't split() find \n?
|
|
| jshock 2007-01-31, 6:59 pm |
| I am writing a script to read the metadata from a bunch of mp4 flies
in a directory, then grab bits of info from the metatags. I am using
system("mdls $filename") to read the metadata.
The problem I am having is that I can't seem to split the metadata
into individual lines. I am using split(/\n/, $metadata) to split by
EOL characters, but it doesn't work.
I've tried replacing \n with other EOL characters: \lf, \cr, \nel,
\ff, \ls, ps -- but nothing seems to work. What am I doing wrong?
I've got a test print statement at the bottom that's supposed to ad
the text "EOL" at the end of each line. Here's my script so far:
#!/usr/bin/perl -w
###############
# Init Variables
$directory = "/Users/joe/Movies/Already Backed Up/";
###############
# Read the directory contents
opendir(DOCS, $directory); #opens the directory
@rawContents = readdir(DOCS);
foreach(@rawContents) {
if($_ !~ /^\./) { #Ignore files that start with a period.
push(@fileNames, $_); #Create a new array with just the live files
}
}
###############
# Convert filenames to unix paths
foreach(@fileNames) {
push(@filePath, $directory.$_); #Add the directory path to the file
names
}
foreach(@filePath) {
($_ =~ s/ /\\ /g);
push(@unixPaths, $_);
}
###############
# Get the metada
$metadata = system("mdls $unixPaths[0]"); #Run unix command mdls.
@metadataArray = split(/\n/, $metadata);
###############
# Test print
foreach(@metadataArray) {
print("$_ EOL\n");
}
For the record -- I'm a perl newbie using MacBook Pro OS 10.4.8. I'm
running perl scripts via terminal/bash.
| |
| Purl Gurl 2007-01-31, 6:59 pm |
| jshock wrote:
> The problem I am having is that I can't seem to split the metadata
> into individual lines. I am using split(/\n/, $metadata) to split by
> EOL characters, but it doesn't work.
> I've tried replacing \n with other EOL characters: \lf, \cr, \nel,
> \ff, \ls, ps -- but nothing seems to work. What am I doing wrong?
> $metadata = system("mdls $unixPaths[0]"); #Run unix command mdls.
> @metadataArray = split(/\n/, $metadata);
Examine your input data to verify a multi-line format, this is,
be sure line breaks are there as expected.
> For the record -- I'm a perl newbie using MacBook Pro OS 10.4.8
@metadataArray = split(/\r/, $metadata);
Use "\r" for a Macintosh.
You can test to be sure,
$metadata =~ tr/\r/\n/;
This will convert \r to \n for your data.
Another method to test and confirm,
$metadata =~ s/\r/#\r/g;
You should see a pound sign (#) at the end of each line.
Other similar tests will verify what you have at
the end of each line.
Unix - \n
Windows - \r\n
Macintosh - \r
Purl Gurl
| |
| Paul Lalli 2007-01-31, 6:59 pm |
| On Jan 31, 4:05 pm, "jshock" <joeshoc...@gmail.com> wrote:
> The problem I am having is that I can't seem to split the metadata
> into individual lines.
No, that is massively not the problem you're having.
> $metadata = system("mdls $unixPaths[0]"); #Run unix command mdls.
What, exactly, do you think should be in $metadata right now? You
seem to be thinking $metadata should contain the output of your mdls
program. Please read the documentation for the function you're
using:
perldoc -f system
See also:
perldoc -q output
Paul Lalli
| |
| jshock 2007-01-31, 9:59 pm |
| On Jan 31, 6:52 pm, "Paul Lalli" <mri...@gmail.com> wrote:
>
> What, exactly, do you think should be in $metadata right now? You
> seem to be thinking $metadata should contain the output of your mdls
> program. Please read the documentation for the function you're
> using:
I think $metadata contains the output of my mdls program. If I run the
Test Print to print $metadata I, I get a listing of the metatags, the
same as if I ran mdls from the terminal. Here's a shortened example of
the results:
kMDItemAttributeChangeDate = 2007-01-17 19:44:28 -0600
kMDItemAudioBitRate = 121.356
kMDItemAudioChannelCount = 2
kMDItemCodecs = ("MPEG-4 Video", AAC)
kMDItemDisplayName = "Jacks Birthday Party.mov"
kMDItemDurationSeconds = 7737.396303854875
kMDItemFSName = "Jacks Birthday Party.mov"
kMDItemPixelHeight = 400
kMDItemPixelWidth = 720
kMDItemTotalBitRate = 1062.912
kMDItemVideoBitRate = 944.576
0 EOL
See, the script places the EOL at the end of the block. I just cant
figure out how to break the block into individual lines.
| |
| jshock 2007-01-31, 9:59 pm |
| On Jan 31, 3:21 pm, Purl Gurl <purlg...@purlgurl.net> wrote:
> $metadata =~ s/\r/#\r/g;
> You should see a pound sign (#) at the end of each line.
> Other similar tests will verify what you have at
> the end of each line.
>
> Unix - \n
> Windows - \r\n
> Macintosh - \r
Thanks Purl Gurl. I gave this a shot, but no luck. I suspect that Mac
Aqua apps us /r, but that the mdls run from macs bash terminal
probablu uses the unix \n. I gave \r a shot, and also tried replacing
th \r with \n, but don't seem to be having much luck.
| |
| Purl Gurl 2007-01-31, 9:59 pm |
| jshock wrote:
> Purl Gurl wrote:
[color=darkred]
> Thanks Purl Gurl. I gave this a shot, but no luck. I suspect that Mac
> Aqua apps us /r, but that the mdls run from macs bash terminal
> probablu uses the unix \n. I gave \r a shot, and also tried replacing
> th \r with \n, but don't seem to be having much luck.
I will urge you again to examine your data to be sure
results returned are precisely what you expect.
Slip this second line below into your code:
$metadata = system("mdls $unixPaths[0]"); #Run unix command mdls.
print "This is my returned data: BEGIN $metadata END";
I am quite sure you will make an important discovery.
Purl Gurl
| |
| Uri Guttman 2007-01-31, 9:59 pm |
| >>>>> "j" == jshock <joeshockey@gmail.com> writes:
j> On Jan 31, 3:21 pm, Purl Gurl <purlg...@purlgurl.net> wrote:[color=darkred]
j> Thanks Purl Gurl. I gave this a shot, but no luck. I suspect that Mac
j> Aqua apps us /r, but that the mdls run from macs bash terminal
j> probablu uses the unix \n. I gave \r a shot, and also tried replacing
j> th \r with \n, but don't seem to be having much luck.
you should ignore moronzilla in the future for your own good. as usual
it totally missed your problem. paul didn't and you didn't rtfm as he
suggested. look CAREFULLY at the docs for system and you will learn it
is not the function you want. then read this:
perldoc -q 'get the output'
as usual the FAQ has the answer. please consult it first before you ask
here. it will be faster and you will get correct answers.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Purl Gurl 2007-01-31, 9:59 pm |
| Uri Guttman wrote:
> jshock writes:
[color=darkred]
> you should ignore moronzilla in the future for your own good. as usual
> it totally missed your problem. paul didn't and you didn't rtfm as he
> suggested. look CAREFULLY at the docs for system and you will learn it
> is not the function you want. then read this:
Purl Gurl writes over two previous articles:
"Examine your input data to verify a multi-line format, this is,
be sure line breaks are there as expected."
"Slip this second line below into your code:
print "This is my returned data: BEGIN $metadata END";
I am quite sure you will make an important discovery."
Uri, you are the best known ignorant troll of the Perl Community!
For so many years you provide the best comedy possible for those
who are relatively intelligent.
Ha! What a silly twit of a troll are you!
Purl Gurl
| |
| Paul Lalli 2007-01-31, 9:59 pm |
| On Jan 31, 8:52 pm, "jshock" <joeshoc...@gmail.com> wrote:
> On Jan 31, 6:52 pm, "Paul Lalli" <mri...@gmail.com> wrote:
>
>
>
> I think $metadata contains the output of my mdls program
You think wrong. Did you read the documentation I told you to run?
>. If I run the
> Test Print to print $metadata I, I get a listing of the metatags,
No. You get that list printed to STDOUT because the program prints it
to STDOUT, not because you printed it to STDOUT. Remove the print
statement, and you'll still see it.
> the
> same as if I ran mdls from the terminal. Here's a shortened example of
> the results:
Which completely obliterates the point, by "shortening" your
results.
Read the documentation, stop assuming.
system() returns the exit code, not the output.
Paul Lalli
| |
| jshock 2007-01-31, 9:59 pm |
| On Jan 31, 8:44 pm, "Paul Lalli" <mri...@gmail.com> wrote:
> On Jan 31, 8:52 pm, "jshock" <joeshoc...@gmail.com> wrote:
>
>
>
>
>
> You think wrong. Did you read the documentation I told you to run?
>
>
> No. You get that list printed to STDOUT because the program prints it
> to STDOUT, not because you printed it to STDOUT. Remove the print
> statement, and you'll still see it.
>
>
> Which completely obliterates the point, by "shortening" your
> results.
>
> Read the documentation, stop assuming.
>
> system() returns the exit code, not the output.
>
> Paul Lalli
Yeah, I finally got it to work by using backticks instead of sytem().
Thanks.
|
|
|
|
|