#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2

    How to print if input is not fixed?


    Hi All,

    I have one .CSV (file1.csv) file:

    John,Mayor,,,Jazz,
    ,Kate,,Jonny,
    ,,Jake,,
    (Here number of commas(",") are not same, they can vary)

    I want output as:
    Hi (to firstname if any), Hello(to secondname if any), GM(to thirdname if any), GA (to fourthname if any), GE (to fifthname if any)

    i.e.
    Hi John, Hello Mayor, GE Jazz
    Hello Kate, GA Jonny
    GM Jake
    ##################################################################
    Please guide me!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    You might split the line into an array (split on commas).

    Then add the greeting to each element of the array if it is defined and not the empty string. Then print the non empty elements.Something like this:

    Perl Code:
    my @greetings = qw /Hi Hello GM GA GE/;
    while (my $line = <>) {
         my @names = split /,/, $line;
         foreach  (0..4) {
              next unless defined $names[$_];
              $names[$_] =~ s/ +//g;
              $names[$_] = "$greetings[$_] $names[$_]" if $names[$_];
         }
         my $output = join ", ", grep {$_ ne ""} @names;
    }



    For the first line, this prints:

    Code:
    Hi John, Hello Mayor, GE Jazz
    Last edited by Laurent_R; April 15th, 2013 at 03:34 PM. Reason: Added checks on not defined names and removing white spaces
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Laurent_R
    You might split the line into an array (split on commas).

    Then add the greeting to each element of the array if it is defined and not the empty string. Then print the non empty elements.Something like this:

    Perl Code:
    my @greetings = qw /Hi Hello GM GA GE/;
    while (my $line = <>) {
         my @names = split /,/, $line;
         foreach  (0..4) {
              next unless defined $names[$_];
              $names[$_] =~ s/ +//g;
              $names[$_] = "$greetings[$_] $names[$_]" if $names[$_];
         }
         my $output = join ", ", grep {$_ ne ""} @names;
    }



    For the first line, this prints:

    Code:
    Hi John, Hello Mayor, GE Jazz
    Great! Thank you Laurent_R.
  6. #4
  7. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    Text::CSV might be worth a look here as well
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Axweildr
    Text::CSV might be worth a look here as well

    Hi Axweildr,
    Thanks for your reply.
    I am trying with another approach.
    If I get any doubt,I will tell you.
    Anyways thank you again.

IMN logo majestic logo threadwatch logo seochat tools logo