Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > A little perl help please...

A little perl help please...
Thread Tools
Senior User
Join Date: Mar 2002
Location: Chicago, IL
Status: Offline
Reply With Quote
Jun 24, 2004, 06:08 PM
 
I'm rather new to Perl, and I'm having trouble with some simple code:

[php]
$file = 'file_list.txt';
open(INFO, $file);
@files = <INFO>;
close(INFO);

foreach $old_name (@files)

{
print $old_name;
@old_pieces = split(/-/, $old_name);

foreach $piece ($old_pieces)

{
print $piece;
print " ";
# do something useful here...
}
}
[/php]

file_list.txt contains a bunch of file names, like such:
001001-01a-copy_of.dwg
001001-01b-copy_of.dwg
001001-01d-copy_of.dwg
001001-01e-copy_of.dwg
001005-01-copy_of.dwg

What I'm trying to do is split each filename into elements of the array $old_pieces using the dash in the name.

For some reason though, my regex statement doesn't seem to want to match anything in those lines....any ideas? The dash above (/-/) is actually escaped, even though it doesn't show up in the forum- I tried it without too and still no luck.

Thanks
We need less Democrats and Republicans, and more people that think for themselves.

infinite expanse
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Jun 24, 2004, 07:06 PM
 
Originally posted by york28:

[php]
$file = 'file_list.txt';
open(INFO, $file);
@files = <INFO>;
close(INFO);

foreach $old_name (@files)

{
print $old_name;
@old_pieces = split(/-/, $old_name);

#foreach $piece ($old_pieces) $ = Wrong!
foreach $piece (@old_pieces) # @ = Right!

{
print $piece;
print " ";
# do something useful here...
}
}
[/php]
I think you just had a minor syntax error in the inner foreach loop (corrected above). Everything else looks good to me.
     
york28  (op)
Senior User
Join Date: Mar 2002
Location: Chicago, IL
Status: Offline
Reply With Quote
Jun 25, 2004, 07:40 AM
 
Oh wow, not sure how I missed that. Thanks a lot! I'll try it out when I get back to work today.
We need less Democrats and Republicans, and more people that think for themselves.

infinite expanse
     
Mac Elite
Join Date: Jun 2001
Location: Dundee, Scotland
Status: Offline
Reply With Quote
Jul 9, 2004, 10:42 AM
 
perhaps you should put
Code:
use strict;
at the top and use perl with the -W (or -w) flag to get warnings
i.e.
Code:
#!/usr/bin/perl -w
this should point stuff like this out
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2