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

SOLVED! Simple school-boy error. Was an issue with Windows EOL instead of 'Nix. Thanks to those who attempted to assist :)

Hi guys, I'm having issues with something which should be extremely simple, but is completely doing my head in.

I'm reading in a data file, which consists of 5 fields, separated by colons. I need to add a couple of zeros at the end of two of the fields (they are date/time fields, which need to have the "seconds" added to them for compliance with importation rules). The first instance of me doing this works fine, but for some reason the second instance, on the last field, is doing some funky stuff.</>

So - this works for testing:

my $data_fix = "00"; while (<DATA>) { my($data1,$data2,$data3,$data4,$data5) = split(/:/); chomp($data5); $data5 = $data5 . $data_fix; $data_line = $data1 . ":" . $data2 . "00" . ":" . $data3 . ":" . $ +data4 . ":" . $data5 . "00" . "\n"; print $data_line; } __DATA__ o:200806050852:b7199767:b24211012077030:200902232359 o:201103251457:b6235734:b2421100997663:201204022359 o:201403101021:b8015840:b24211013181843:201404072359 o:201407291225:b8759124:b24211001018862:201408262359
OUTPUT: o:20080605085200:b7199767:b24211012077030:20090223235900 o:20110325145700:b6235734:b2421100997663:20120402235900 o:20140310102100:b8015840:b24211013181843:20140407235900 o:20140729122500:b8759124:b24211001018862:20140826235900

And this doesn't work:

my $data_fix = "00"; while (<>) { my($data1,$data2,$data3,$data4,$data5) = split(/:/); chomp($data5); $data5 .= $data_fix; $data_line = $data1 . ":" . $data2 . "00" . ":" . $data3 . ":" . $ +data4 . ":" . $data5 . "\n"; print $data_line; }
OUTPUT: (note the leading "o:" has been replaced with the "00" which i +s meant to be on the end of the last field, for some reason??) 0020080605085200:b7199767:b24211012077030:200902232359 0020110325145700:b6235734:b2421100997663:201204022359 0020140310102100:b8015840:b24211013181843:201404072359 0020140729122500:b8759124:b24211001018862:201408262359

What am I missing here, please??

UPDATE:

It would seem that it has something to do with the way I am accepting the data into the script. When running the script as shown above the the __DATA__ tag internal, it works fine. But when passing that exact same data in with a "cat" call, and changing my while(<DATA>) to while(<>), it breaks...

I'll clean up the above to hopefully simplify where I'm at now.

Replies are listed 'Best First'.
Re: String concatenation issues with 00?
by kcott (Archbishop) on Nov 19, 2015 at 03:05 UTC

    G'day bobdabuilda,

    The code and output you've posted do not match. Running the exact code you posted, I get this output:

    o 200806050852 b7199767 b24211012077030 200902232359 o 200806050852 b7199767 b24211012077030 20090223235900 o:20080605085200:b7199767:b24211012077030:20090223235900 o 201103251457 b6235734 b2421100997663 201204022359 o 201103251457 b6235734 b2421100997663 20120402235900 o:20110325145700:b6235734:b2421100997663:20120402235900 ...

    Presumably, your dodgy OUTPUT: ... was produced by other code we've not seen.

    Given the type of data you're working with, I recommend you take a look at Text::CSV.

    — Ken

      The output is like that because foreach line of <DATA> he is printing it three different times. Go ahead and remove the first two print statements and behold the desired output!

      EDIT: nevermind. Maybe he is calling the wrong script. I do it all the time! Ill copy a script to another directory and make updates to it, then save and run it while still in the other directory, and wonder why the output never changes haha

        Between reading your response and replying, you've updated your post. That's fine but just to clear up any misunderstanding:

        I was demonstrating that the posted code did not produce the posted output. In fact, it did produce the desired output (along with a couple of test print lines).

        — Ken

Re: String concatenation issues with 00?
by james28909 (Deacon) on Nov 19, 2015 at 02:55 UTC
    It seems to work fine for me. As soon as i copied your script, I turned off printing except for the last one and seems to have your desired output, maybe i am missing something?
    my $data_fix = "00"; while (<DATA>) { my($data1,$data2,$data3,$data4,$data5) = split(/:/); chomp($data5); $data5 .= $data_fix; $data_line = $data1 . ":" . $data2 . "00" . ":" . $data3 . ":" . $ +data4 . ":" . $data5 . "\n"; print $data_line; } __DATA__ o:200806050852:b7199767:b24211012077030:200902232359 o:201103251457:b6235734:b2421100997663:201204022359 o:201403101021:b8015840:b24211013181843:201404072359 o:201407291225:b8759124:b24211001018862:201408262359
    Definately outputs:
    C:\Users\James\Desktop\perlmonks>test.pl o:20080605085200:b7199767:b24211012077030:20090223235900 o:20110325145700:b6235734:b2421100997663:20120402235900 o:20140310102100:b8015840:b24211013181843:20140407235900 o:20140729122500:b8759124:b24211001018862:20140826235900 C:\Users\James\Desktop\perlmonks>
    Maybe it is time to take a step back, go get yourself something to drink or some fresh air ;)

      Hi James, thanks for that. It would seem that, in my attempt at redacting some of the data used (manually, rather than doing it properly and doing up some real test data), I accidentally fixed my issue.

      Now to go back over my real code and see what's different between that and what I posted here... I actually spent quite a bit of time trying different options with the concatenation, chomp, etc. and just couldn't get it right. Seems all I needed to do was give up ;)

        Heres another way to do it, which seems more legible:
        use strict; use warnings; while (<DATA>){ chomp; my @line = split /:/; print "$line[0]:$line[1]00:$line[2]:$line[3]:$line[4]00\n"; } __DATA__ o:200806050852:b7199767:b24211012077030:200902232359 o:201103251457:b6235734:b2421100997663:201204022359 o:201403101021:b8015840:b24211013181843:201404072359 o:201407291225:b8759124:b24211001018862:201408262359
        Output:
        o:20080605085200:b7199767:b24211012077030:20090223235900 o:20110325145700:b6235734:b2421100997663:20120402235900 o:20140310102100:b8015840:b24211013181843:20140407235900 o:20140729122500:b8759124:b24211001018862:20140826235900