>>>>> "G" == Grant <[email protected]> writes:
G> The value of $string could include dashes or not. If it is, I'd like
G> the value of $foo to be set to the portion of the string to the left
G> of the first dash. If not, I'd like the value of $foo to be null.
G> I'm doing the following, but $foo is equal to "1" if there are no
G> dashes in the string:
G> $foo = (split('-',$string)),[0];
that has a basic syntax error in it. did you run it with warnings
enabled? you should always run with warnings as it will help you fix
problems like this:
perl -wle '$s = "abc" ; $foo = (split("-",$s)),[0]; print $foo'
Use of implicit split to @_ is deprecated at -e line 1.
Useless use of anonymous list ([]) in void context at -e line 1.
the , is incorrect there. where did you learn that from? when you slice
a list you put the [] immediately after the closing ).
even with that fix it still isn't going to work.
perl -wle '$s = "a-bc" ; $foo = (split("-",$s))[0]; print $foo'
a
perl -wle '$s = "abc" ; $foo = (split("-",$s))[0]; print $foo'
abc
split returns the whole string as the only result field if it finds no
delimiters. a correct solution is to use a regex and check for matching:
$foo = $string =~ /^([^-])+-/ ? $1 : '' ;
that will grab something from the start to the first - and grab it. if
it matched it will assign it to $foo, otherwise assign ''. (and '' is
called the null string, not null. perl has no null things unlike
databases).
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/