Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

string manupulation

by venki (Acolyte)
on Sep 05, 2002 at 17:39 UTC ( [id://195450]=perlquestion: print w/replies, xml ) Need Help??

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


I need your valuable suggestion perl monks for the below:

If i have a string like the below:

my $local = 'one:two:three';

I want to split the string and store in a array like the below
temp[0] ='one'; temp[1] = 'two'; temp[2] = 'three';
Any suggestion!

Replies are listed 'Best First'.
Re: string manupulation
by thelenm (Vicar) on Sep 05, 2002 at 17:42 UTC
    I want to split the string

    There's your answer. :-) split does the job, like this:

    my $local = 'one:two:three'; my @temp = split /:/, $local;

    -- Mike

    --
    just,my${.02}

      Thank you Mike
Re: string manupulation
by demerphq (Chancellor) on Sep 05, 2002 at 18:07 UTC
    sub split_by_colon { my $string=shift; my @parts; while (length $string) { push @parts,"" unless @parts; my $char=substr($string,0,1,""); unless ($char eq ":") { $parts[-1].=$char; } else { push @parts,""; } } return @parts; }
    Or you could look in perlfunc for a better way

    ;-)

    Yves / DeMerphq
    ---
    Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)

      lol =)

      -Waswas
Re: string manupulation
by mp (Deacon) on Sep 05, 2002 at 17:43 UTC
    The builtin split function will do this easily. For documentation on it, enter:
    perldoc -f split
    in a shell window.
Re: string manupulation
by BronzeWing (Monk) on Sep 05, 2002 at 19:01 UTC

    Just because using split() is way too easy of an answer:

    @temp = $local =~ m/(?:^|:)([^:]*)/g;

    BronzeWing

      Regex Hacking, With A Smile. :^)

      Makeshifts last the longest.

Re: string manupulation
by metlhed_ (Beadle) on Sep 05, 2002 at 17:45 UTC

    You want to use split. like this:

    my $line = 'one:two:three'; my @array = split(/:/, $line);

    That will do exacly what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://195450]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found