RE: regex help

2007-08-08 Thread Dan Sopher
This works in a one-liner:

$string =~ s/^\s*(.*\S)\s*$/$1/;

Cheers!

-Dan



-Original Message-
From: Dr.Ruud [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 08, 2007 2:05 PM
To: beginners@perl.org
Subject: Re: regex help

Jeff Pang schreef:
> John W. Krahn:
>> Tony Heal:

>>> Why doesn't this work? I want to take any leading 
>>> or trailing white spaces out. 
>> 
>> perldoc -q "How do I strip blank space"
> 
> Or generally it could be done by,
> $string =~ s/^\s+|\s+$//g;

The g-modifier doesn't mean "generally" nor "good". ;-) 
Please see the suggested perldoc text for the proper ways. 

I like to use:

  s/^\s+//, s/\s+$// for $string;

but

  $string =~ s/^\s+//;
  $string =~ s/\s+$//;

may be slightly faster.
(like because no localization of $_) 

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




File::Find help

2007-08-16 Thread Dan Sopher


Hello. The following code example creates a list of regular files in a
directory. Using File::Find, I'm unable to localize an array to hold the
list of files. Is there a way to create the list with a localized array?
TIA.



#!/usr/bin/perl -w
## Create a list of regular files in a directory.

use strict;
use File::Find;

my @list; ## Don't want this here
if (1) {
## I'd like @list to be localized to this scope,
## and not in main

find ( { wanted=>\&found, no_chdir=>1 }, "/var/SAMPLES" );

print @list;
}


sub found {
push @list, $_ if -f $_;
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: File::Find help

2007-08-16 Thread Dan Sopher
Thanks for everyone's replies. It helped a lot. It ended up like this.
My next question is the use of the array reference, @list. Is it bad
form?


#!/usr/bin/perl -w

use strict;
use File::Find;

if (1) {
  my $dir = "/var/SAMPLES";
my @list;

find ( { wanted=>sub { &found([EMAIL PROTECTED]) }, no_chdir=>1 }, $dir 
);
print @list;
}

sub found {
my $list = shift;
## Do some string processing here...
push @{$list}, $_ if -f $_;
return;
}



-Original Message-
From: Dan Sopher [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 16, 2007 3:46 PM
To: beginners@perl.org
Subject: File::Find help



Hello. The following code example creates a list of regular files in a
directory. Using File::Find, I'm unable to localize an array to hold the
list of files. Is there a way to create the list with a localized array?
TIA.



#!/usr/bin/perl -w
## Create a list of regular files in a directory.

use strict;
use File::Find;

my @list; ## Don't want this here
if (1) {
## I'd like @list to be localized to this scope,
## and not in main

find ( { wanted=>\&found, no_chdir=>1 }, "/var/SAMPLES" );

print @list;
}


sub found {
push @list, $_ if -f $_;
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




foreach and map..how are they different?

2007-08-24 Thread Dan Sopher
Aside from the syntax, is there a difference in the way 'map' and
'foreach' process?


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




& and >>

2007-09-04 Thread Dan Sopher
Regarding the following document:

 

http://perldoc.perl.org/functions/system.html

 

1.  What does ($? & 127) mean?
2.  What does $? >> 8  mean?

 

 

Example from the document:

 

if ($? == -1) {
 print   "failed to
execute: $!\n";
}
elsif ($? & 127) {
 printf   "child died
with signal %d, %s coredump\n",
 ($? & 127),  ($? & 128) ? 'with' : 'without';
}
else {
 printf   "child
exited with value %d\n", $? >> 8;

}

 

Thanks in advance,

 

Dan