Juhi has asked for the wisdom of the Perl Monks concerning the following question:

hey there I am reading a file line by line... each line is stored in a scalar variable($LINE) and then i need to extract all the characters from the line till the occurrance of the first ":" in the line and also extract all chracters between the fourth ":" and the next "," in the same line.................. can anyone tell me how to do that.. i believe substr($VARNAME,N,M) returns a substring between the Nth and Mth column in a line.. So if i could just read each character in a line and count the column numbers of the first and fourth colon(":") i guess this'd be possible...but i have no clue as to how i can read the variable $LINE character by character

2006-06-04 Retitled by planetscape, as per Monastery guidelines

( keep:0 edit:12 reap:0 )

Original title: 'selecting characters from a varialble'

Replies are listed 'Best First'.
Re: selecting characters from a variable
by ptum (Priest) on Jun 02, 2006 at 13:36 UTC

    It sounds to me as though you really want to do a 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 split without a delimiting argument, which will cause your string to be split into one-character array elements.

    Update: As the man without a Pony indicates below, what I mean when I say 'without a delimiting argument' is this:

    my @tokens = split //,$line;

    No good deed goes unpunished. -- (attributed to) Oscar Wilde

      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>

Re: selecting characters from a variable
by thundergnat (Deacon) on Jun 02, 2006 at 15:20 UTC
    use warnings; use strict; while ( my $line = <DATA> ) { 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 eve +n more:Fifth
Re: selecting characters from a variable
by dsheroh (Monsignor) on Jun 02, 2006 at 15:14 UTC
    Assuming those two spans of characters are all you're interested in (rather than the entire line as a series of :-delimited fields), then it sounds to me like you're looking for a regex-based solution, such as:
    $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.)

Re: selecting characters from a variable
by jesuashok (Curate) on Jun 02, 2006 at 13:58 UTC
    Hi

    $line = "one:two:three:four:five:five"; my ( $one,$two) = (split(/:/,$line))[0,3]
    I am not able to understnad the following line :-
    and the next "," in the same line

    If you give me the proper input I can answer for that.

    "Keep pouring your ideas"