Query about Split function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    Query about Split function

    Hi all
    I have a file with below contents I need to store all the values in leftside into an array and right side into another array.
    Filename:list.t xt

    ram,kriz
    rakesh,ssss
    perl,unix

    If we assume the array name as ltemp then ltemp[0] should have "ram"
    ltemp[1] should have rakesh and ltemp[2] should have "perl".

    I tried the same,

    [CODE=perl]
    open(FP,"list.t xt") or die "cannot open the list file";
    while (<FP>) {
    $temp = $_;
    @ltemp = split(/,/ ,$temp);
    print @ltemp[0];
    }
    close(FP);
    [/CODE]

    here ltemp[0] contains all the three elements and ltemp[1] has "kriz,rakesh,un ix"

    How can I store each value in seperate array Index..

    Hope you can get my question
    Last edited by miller; Jun 4 '07, 04:55 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Hello kriz,

    I believe that makes sense enough. However, if you really want those values in two different arrays, why have you not defined two different arrays? Putting that question aside, maybe this is what you are aiming at.

    [CODE=perl]
    my (@array1, @array2);

    my $file = 'list.txt';
    open(FP, $file) or die "Cannot open $file: $!";
    while (<FP>) {
    my ($col1, $col2) = split ',';
    push @array1, $col1;
    push @array2, $col2;
    }
    close(FP);

    print @array1 . "\n";
    print @array2 . "\n";
    [/CODE]

    - Miller

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      here ltemp[0] contains all the three elements and ltemp[1] has "kriz,rakesh,un ix"
      Actually your code is not storing anything except the last line of the file in the @ltemp array. I have a feeling you really want to use a hash but maybe not.

      Comment

      Working...