2006-06-04 Retitled by planetscape, as per Monastery guidelines
Original title: 'selecting characters from a varialble'
It sounds to me as though you really want to do a [perldoc://split], parsing your string into tokens, delimited by the ':' character.
my @tokens = split /:/,$line;
If you really do want to split up the line into an array of characters, you can simply use the [perldoc://split] without a delimiting argument, which will cause your string to be split into one-character array elements.
Update: As [Fletch|the man without a Pony] indicates below, what I mean when I say 'without a delimiting argument' is this:
my @tokens = split //,$line;
You still need to give a pattern to split on which matches the null string (// or '') to split to characters; omitting both the pattern and what to split splits $_ on whitespace (at least in list context; in scalar context it splits $_ on whitespace into @_ and you'll get griped at as it's deprecated behavior). See the docs for split for more details.</pedant>
$line = "one:two:three:four:five:five"; my ( $one,$two) = (split(/:/,$line))[0,3]I am not able to understnad the following line :-
$LINE =~ /^([^:]*):[^:]:[^:]:[^:]:([^,:]*)/; $up_to_first_colon = $1; $fourth_colon_to_next_comma = $2;The ^([^:]*): captures all of the non-colon characters ([^:]*) from the start of the line (^) until the first colon and places it in $1.
Each [^:]: skips over a group of non-colon characters, ending with a colon.
Finally, ([^,:]*) captures the group of non-colon, non-comma characters following the fourth colon and places it in $2. (Change it to ([^,]*) if the second field should only be terminated by a comma and not by a colon.)
use warnings;
use strict;
while ( my $line = ) {
my ( $first, $fourth ) = (split(/:/, $line))[0,3];
$fourth =~ s/,.+$//;
print "$first, $fourth\n";
}
__DATA__
First column:Second:Third:Fourth Column, with some extra crap:Fifth
First column:Second:Third:Fourth Column, with some extra crap, and even more:Fifth
perlmonks.org content © perlmonks.org and esper, Fletch, jesuashok, Juhi, ptum, thundergnat
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03