> Hi ppl,
> 
> I have to split a file in pieces and I place every segment in an array.
> Each segment begins with digit(s). What I want to do is to parse the
> array, and for each segement, record its first digits into a scalar so I
> can do further manipulations on it (actually I only print the scalar
> into a file).
> 
> What I do right now is :
> 
> my $digit = 0;
> for (@commands)
> {
>     $digit =~ /^\d+/;
>     print FILE "DIGIT = $digit \n";
> }
> 

[snip]

There was another response but I think it can be clearer.  Right now you
are running a test in void context on your string and not capturing
either the value itself or the return.  Generally you want to test the
string, capture your value, and have a warning or some such if it fails.
 I also prefer we dispense with the use of $_.  For instance,

foreach my $cmd (@commands) {
  if ($cmd =~ /^(\d+)/) {
     print FILE "DIGIT = $1\n";
  }
  else {
     warn "Invalid command: $cmd\n";
  }
}

If you do not place the regex in a test then you will end up with
"empty" values in your file.  The parentheses around the \d+ tell the
regex to capture the value into the first holder $1.

perldoc perlretut
perldoc perlre

http://danconia.org


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


Reply via email to