Link to home
Start Free TrialLog in
Avatar of chuckster_eh

asked on

strings with embedded spaces

I am reading a flat file into an xml using perl.  The file has strings with spaces in it.  How do I deal with that.  I want to pick up someones first name and last name as one string and its treating it as two.
Avatar of kaufmed
Flag of United States of America image

Is this a "fixed-width" or "delimited" flat file?
Are you reading xml using some kind of xml module?

If first name and last name are two different attributes, you can concatenate them.

What do you mean by "deal with" spaces?
Avatar of chuckster_eh


I have a text file with 2 elements firstname and last name

Joe Smith

When I read the file using perl to insert into an xml its only picking up "Joe" or "Smith".  I have tried single quotes 'Joe Smith' and double quotes "Joe Smith"
How are you reading the file?  Please show the related code.
sub main
      # Note: this could be a full file path
      my $filename = "fileB.csv";

      open(INPUT, $filename) or die "Cannot open $filename";

      # Read the header line.
      my $line = <INPUT>;

      # Read the lines one by one.
      while($line = <INPUT>)
            # Just display the line for now.
            print "http://localhost:8080/XMLPost/post?userId=$line";
            system("curl http://localhost:8080/XMLPost/post?userId=$line");
            sleep 1;


It should work.  Are you by any chance changing $/ special variable or the file format is special somehow.
I tested it as well, it works for me, except that you should have new line as well after your  first print statement.

So either you $/ is unset somehow.  You can set it to

$/ = "\n";

Or your file has something weird in it.
I figured it out.  It was a URL encoding issue with the space using Curl.     how should I deal with the points for your time?
Avatar of farzanj
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Giving farzanj the points for his efforts.  Thank you!!
Submit what fixed it, accept that as answer and my response as assist.  that should do it.