parsing CSV File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnsreee
    New Member
    • Apr 2007
    • 34

    parsing CSV File

    Hi all ,

    I have the following string
    "raj,234,,,asur t,valol"
    I am using the following code to parse that line into an array.
    Code:
    push(@value, $+) while $tmp2[2] =~ m{"([^\"\\]*(?:\\.[^\"\\]*)*)",?  |  ([^,]+),?   | , }gx;
    push(@value, undef) if substr($tmp2[2], -1,1) eq ',';
    but I want to take the comma into the array.like "raj , asurt valol "
    But the array is filling as bellow
    " raj undef undef asurt valol"

    Can any one help me to read this String.

    Thanks
    Naveen
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Hello Naveen,

    First of all, parsing a CSV file by hand is rarely a good idea. Use a cpan module like Text::CSV.

    However, to address the problem of undefined files, just use a grep function.

    [CODE=perl]
    @x = ('raj',undef,un def,'asurt','va lol')
    @y = grep {defined} @x; # Equals ('raj','asurt', 'valol')
    [/CODE]

    - Miller

    Comment

    • pnsreee
      New Member
      • Apr 2007
      • 34

      #3
      Hi Miller,
      Thanks for Your replay.
      But I want the output like - ('raj' ',' 'asurt' 'valol') when ever I am giving Input like bellow (raj,,,asurt,va lov)

      Is it possable to get output like that.

      Regards
      Naveen

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Naveen,

        To put it simply, anything is possible. But certain specifications make a lot more sense than others. This one seems rather wonky.

        Nevertheless, if that's really what you want, the follow regex would work a lot better than what you're doing.

        [CODE=perl]
        my $s = "raj,234,,,asur t,valol"
        my @value = ($s =~ /(.+?)(?:,|$)/g);
        print join ' ', @value; # Ouputs: raj 234 , asurt valol
        [/CODE]

        - Miller

        Comment

        • pnsreee
          New Member
          • Apr 2007
          • 34

          #5
          Hi Miller

          If i use this expression if the string is "raj,234,,asurt,valol" like this i am getting output like this "raj 234 ,asurt valol".
          using regex i cant distuingeshe between ",,," and ",,".

          Thanks
          Naveen

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            As I said bro,

            Your spec is completely arbitrary and wonky. This is why in the real CSV specification, to include commas, you must enclose them in a string.

            To reinvent the wheel is one thing. To purposefully reinvent the wheel badly is something completely different.

            - M

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              I guess you could do something a bit absurd like this if you really really have to:

              Code:
              my $s = "raj,234,,,asurt,valol";
              $s =~ s/(,,,)/,\[comma\],/g; 
              my @value = map {$_ eq '[comma]' ? ',' : $_} split(/,/,$s);
              print join ' ', @value;

              Comment

              Working...