Forum Moderators: coopster & phranque

Message Too Old, No Replies

Parsing strings into a number of set variables

         

ace2010

11:20 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



I'm new to Perl, but know it has great string capabilities. I would like to know how to pass a string from the command line and break it into several components to pass to other functions.

Example: Parse 123_456_789

so that variable1 = 123, variable2 = 456, variable3 = 789.

thanks for any input here.

mikeyb

9:07 am on Aug 9, 2005 (gmt 0)

10+ Year Member



Hi,

If you know there are always going to be 3 "items" in your input string, you could do the following:

my $string = "123_456_789";
my ($variable1, $variable2, $variable1) = split(/_/,$string);

If there could be a variable number, then split it to an array, then loop thru the array:
my $string = "123_456_789";
my @vars = split(/_/,$string);
foreach (@vars) {
print "$_";
}

Cheers
Mike

ace2010

3:51 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



Thanks Mikey, much appreciated! There will always be a set number of variables, so the first piece of code should work for me.

Thanks again.