Your code doesn't compile. You've left out braces here and there. In the
future, please use WORKING code unless such code is not possible to
create.
On Jun 10, [EMAIL PROTECTED] said:
>$ff="hello fred 12";
>@gg=split(//, $ff);$gglen=@gg;
>$hh=substr($ff, ($gglen-1), 1);$fin="n";$val=1;
First, give your variables decent names. You don't go naming children
"It" and "The Other One".
$str = "hello fred 12";
$char_len = @chars = split //, $str;
$last_char = substr $str, $char_len-1, 1;
$fin = "n";
$val = 1;
But I see something silly here. You've got a string. You split it into
an array. You get the size of the array.
WHY not get the LENGTH of the string?
$str = "hello fred 12";
$len = length $str;
$last_char = substr $str, $len-1, 1;
Better yet, you don't even NEED $len!
$str = "hello fred 12";
$last_char = substr $str, -1, 1;
>if("$hh" =~ /^\d$/ )
> $hh=substr($ff, ($gglen-2), 1);$val=2;$fin="n";
>}
Useless quoting of $hh there. And since $last_char is only ONE character,
the anchors are pretty much redundant.
if ($last_char =~ /\d/) {
$last_char = substr $str, -2, 1;
$val = 2;
$fin = "n";
}
>else{
> $fin="y";$H=substr($ff, 0, ($gglen-1));$T=substr($ff, ($gglen-1), 1);
>print "This is text $H and time $T on 1st line\n";
>}
else {
$fin = "y";
$rest = substr $str, 0, -1;
$time = substr $str, -1;
print "This is text '$rest' and time '$time' on 1st line\n";
}
>if("$fin" eq "n" ) {
>if("$hh" =~ /^\d$/ ) {
> $hh=substr($ff, ($gglen-3), 1);$val=3;
>} else {
> $fin="y";$H=substr($ff, 0, ($gglen-2));$T=substr($ff, ($gglen-2), 2);
>print "This is text $H and time $T on 2nd line\n";
>}
>}
More useless quoting of variables.
if ($fin eq 'n') {
if ($str =~ /\d/) {
$str = substr $str, -3, 1;
$val = 3;
}
else {
$fin = "y";
$rest = substr $str, 0, -2;
$time = substr $str, -2;
print "This is text '$rest' and time '$time' on 2nd line\n";
}
}
>if("$fin" eq "n" )
>
>if("$hh" =~ /^\d$/
>
> $hh=substr($ff, ($gglen-4), 1);$val=4;
>} else
>
>$fin="y";$H=substr($ff, 0, ($gglen-3));$T=substr($ff, ($gglen-3), 3);
>print "This is text $H and time $T on 3rd line ... $H\n";
>}
>}
Ok, I see what you're doing. You want to extract a chunk of digits from
the end of a string.
$str = "hello fred 12";
$str =~ s/(\d+)$// and $num = $1;
That's much shorter. It uses a regular expression to match the last set
of digits at the end of the string, and then removes them from $str, and
puts them into $num.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]