selecting characters from a variable
Juhi
created: 2006-06-02 09:18:40
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'

Re: selecting characters from a variable
created: 2006-06-02 09:36:48

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;

No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re^2: selecting characters from a variable
created: 2006-06-02 11:03:15

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
created: 2006-06-02 09:58:19
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"
Re: selecting characters from a variable
created: 2006-06-02 11:14:18
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
created: 2006-06-02 11:20:55
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