FAQ
Hi,

I've attached a text file containing the original and required format and
avoid the format
being lost by just pasting it in the email body.

The original format is separated by a space, but need to replace the space
with a comma,
so it will become a comma delimited csv file which I will then import to MS
Excel.

I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time" columns
since the dates
there in is in this format "Fri Jun 24 06:37" which also separated by space,
but the spaces in
those columns shouldn't be replaced by a comma. The date format in those
columns should
remain the same.

Also, is it possible to convert this directly into a MS Excel document using
Perl?

Many thanks!

Regards,
Wernher

Search Discussions

  • Dermot at Jun 24, 2011 at 8:19 am

    On 24 June 2011 08:45, Wernher Eksteen wrote:
    Hi,

    I've attached a text file containing the original and required format and
    avoid the format
    being lost by just pasting it in the email body.

    The original format is separated by a space, but need to replace the space
    with a comma,
    so it will become a comma delimited csv file which I will then import to MS
    Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time" columns
    since the dates
    there in is in this format "Fri Jun 24 06:37" which also separated by space,
    but the spaces in
    those columns shouldn't be replaced by a comma. The date format in those
    columns should
    remain the same.

    Also, is it possible to convert this directly into a MS Excel document using
    Perl?
    Yes, you can achieve all your goals with perl. Have a look and
    Text::CSV_XS[1] or Text::CSV[2]. They will read the file correctly and
    write the file out for you with any separator you want. BTW, you want
    quotes around each field so as not to confuse you spreadsheet program.
    Writing the file into XLS format would require another module but I'll
    leave others to make that recommendation.

    Perhaps you could have a look at those, try and write something and
    when, or if, you get stuck, come back to the list showing the code
    you've got and get some pointers.

    Thanks,
    Dermot.


    1) http://search.cpan.org/~hmbrand/Text-CSV_XS-0.82/CSV_XS.pm
    2) http://search.cpan.org/~makamaka/Text-CSV-1.21/lib/Text/CSV.pm
  • Wernher Eksteen at Jun 24, 2011 at 8:53 am
    Hi,

    Thanks for the tip, will try to figure it out on the weekend and come back
    if I'm
    stuck.

    Just a few questions before I try this...

    Does these modules have the ability to add the commas in the way I need them
    to be?

    The CVS_XS.pm module seems more flexible/powerful than the CVS.pm one
    or am I wrong?

    Regards,
    Wernher

    On 24 June 2011 10:19, Dermot wrote:
    On 24 June 2011 08:45, Wernher Eksteen wrote:
    Hi,

    I've attached a text file containing the original and required format and
    avoid the format
    being lost by just pasting it in the email body.

    The original format is separated by a space, but need to replace the space
    with a comma,
    so it will become a comma delimited csv file which I will then import to MS
    Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time" columns
    since the dates
    there in is in this format "Fri Jun 24 06:37" which also separated by space,
    but the spaces in
    those columns shouldn't be replaced by a comma. The date format in those
    columns should
    remain the same.

    Also, is it possible to convert this directly into a MS Excel document using
    Perl?
    Yes, you can achieve all your goals with perl. Have a look and
    Text::CSV_XS[1] or Text::CSV[2]. They will read the file correctly and
    write the file out for you with any separator you want. BTW, you want
    quotes around each field so as not to confuse you spreadsheet program.
    Writing the file into XLS format would require another module but I'll
    leave others to make that recommendation.

    Perhaps you could have a look at those, try and write something and
    when, or if, you get stuck, come back to the list showing the code
    you've got and get some pointers.

    Thanks,
    Dermot.


    1) http://search.cpan.org/~hmbrand/Text-CSV_XS-0.82/CSV_XS.pm
    2) http://search.cpan.org/~makamaka/Text-CSV-1.21/lib/Text/CSV.pm

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

  • Dermot at Jun 24, 2011 at 9:19 am

    On 24 June 2011 09:53, Wernher Eksteen wrote:
    Hi,

    Thanks for the tip, will try to figure it out on the weekend and come back
    if I'm
    stuck.

    Just a few questions before I try this...

    Does these modules have the ability to add the commas in the way I need them
    to be?
    Yes. When you construct a new Text::CSV_XS object, set the sep_char to
    the tab character so that when you read the file in, each value from
    each column is stored correctly. They reset sep_char to ',' when you
    want to send the output to a file.
    The CVS_XS.pm module seems more flexible/powerful than the CVS.pm one
    or am I wrong?
    The _XS means that this module uses C code under the bonnet so it runs
    really fast. If you are on a *nix based PC and have all the necessary
    libraries and a compiler this is probably a better option. It's the
    one I generally use. This module also has a _PP (pure perl) version[1]
    in case you do not have a compiler on your PC.


    1) http://search.cpan.org/perldoc?Text%3A%3ACSV_PP

    HTH,
    Dermot.
  • Jim Gibson at Jun 24, 2011 at 2:47 pm

    At 9:45 AM +0200 6/24/11, Wernher Eksteen wrote:
    Hi,

    I've attached a text file containing the original and required
    format and avoid the format
    being lost by just pasting it in the email body.

    The original format is separated by a space, but need to replace the
    space with a comma,
    so it will become a comma delimited csv file which I will then
    import to MS Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time"
    columns since the dates
    there in is in this format "Fri Jun 24 06:37" which also separated
    by space, but the spaces in
    those columns shouldn't be replaced by a comma. The date format in
    those columns should
    remain the same.

    Since your columns are fixed-width, you can use the unpack function
    to unpack the lines into individual fields. You could also use a
    series of substr calls, but unpack would be less code.

    See 'perldoc -f unpack' for details, and 'perldoc -f pack' for
    template elements describing your data columns.
    Also, is it possible to convert this directly into a MS Excel
    document using Perl?
    Not directly, but once you have the lines unpacked, you can use the
    Spreadsheet::WriteExcel module to create a spreadsheet and populate
    it with your data.
  • Rob Dixon at Jun 25, 2011 at 1:52 pm

    On 24/06/2011 08:45, Wernher Eksteen wrote:
    Hi,

    I've attached a text file containing the original and required
    format and avoid the format being lost by just pasting it in the
    email body.

    The original format is separated by a space, but need to replace the
    space with a comma, so it will become a comma delimited csv file
    which I will then import to MS Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time"
    columns since the dates there in is in this format "Fri Jun 24 06:37"
    which also separated by space, but the spaces in those columns
    shouldn't be replaced by a comma. The date format in those columns
    should remain the same.

    Also, is it possible to convert this directly into a MS Excel document
    using Perl?
    Hey Wernher

    Your data appears to have three or more spaces separating the data
    fields, whereas there is never more than a single space within the data.
    If that is always the case, and there are never any blank fields, we can
    use it to split the line into fields.

    Take a look at the program below.

    HTH,

    Rob


    use strict;
    use warnings;

    while (my $record = <DATA>) {
    chomp $record;
    my @fields = split /[ ]{3,}/, $record;
    print join ',', @fields;
    print "\n";
    }

    __DATA__
    CTX Destination Enabled Connection Sync'ed-as-of-time
    --- --------------------------------------------------------------------- ------- ---------------- ------------------
    1 pool://ZABRYDD01.localdomain/RDP_NEW_REP yes Sat Jun 18 08:56 Fri Jun 24 06:37
    2 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101 yes Sat Jun 18 08:57 Fri Jun 24 08:01
    4 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    5 pool://ZARDPDD01.localdomain/BRYREP yes Sat Jun 18 08:57 Fri Jun 24 07:01
    8 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    10 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    12 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    13 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    --- --------------------------------------------------------------------- ------- ---------------- ------------------

    **OUTPUT**

    CTX,Destination,Enabled,Connection,Sync'ed-as-of-time
    ---,---------------------------------------------------------------------,-------,----------------,------------------
    1,pool://ZABRYDD01.localdomain/RDP_NEW_REP,yes,Sat Jun 18 08:56,Fri Jun 24 06:37
    2,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101,yes,Sat Jun 18 08:57,Fri Jun 24 08:01
    4,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22,yes,Sat Jun 18 08:57,Fri Jun 24 09:00
    5,pool://ZARDPDD01.localdomain/BRYREP,yes,Sat Jun 18 08:57,Fri Jun 24 07:01
    8,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31,yes,Sat Jun 18 08:57,Fri Jun 24 09:00
    10,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421,yes,Sat Jun 18 08:57,Fri Jun 24 09:00
    12,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5,yes,Sat Jun 18 08:57,Fri Jun 24 09:00
    13,dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60,yes,Sat Jun 18 08:57,Fri Jun 24 09:00
    ---,---------------------------------------------------------------------,-------,----------------,------------------
  • Rob Dixon at Jun 25, 2011 at 5:02 pm

    On 24/06/2011 08:45, Wernher Eksteen wrote:

    I've attached a text file containing the original and required
    format and avoid the format being lost by just pasting it in the
    email body.

    The original format is separated by a space, but need to replace the
    space with a comma, so it will become a comma delimited csv file
    which I will then import to MS Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time"
    columns since the dates there in is in this format "Fri Jun 24
    06:37" which also separated by space, but the spaces in those
    columns shouldn't be replaced by a comma. The date format in those
    columns should remain the same.

    Also, is it possible to convert this directly into a MS Excel
    document using Perl?
    Hello again Wernher.

    My first guess for this problem was to build an 'unpack' format based on
    the row of hyphens beneath the header captions. As it may still be
    useful, and well worth studying for similar applications, here is my
    alternative solution doing just that.

    The program grabs everything in the file that is aligned with the row
    of hyphens beneath the captions. It works by finding the first line in
    the file that contains only hyphens or spaces, and scanning that to
    build an unpack string using the @- and @+ arrays that hold the offsets
    of the start and end of the previous successful regex match.

    Unlike my previous solution, empty (all-space) fields are handled
    correctly: the 'A' unpack format discards trailing spaces, and my only
    proviso is that if you ever expect leading spaces in fields they must be
    trimmed explicitly.

    HTH,

    Rob


    use strict;
    use warnings;

    use Fcntl 'SEEK_SET';

    my $format;

    my $fh = *DATA; # Replace with the appropriate 'open my $fh, '<', ... or die $!;

    # Remember the where the file begins, and then build the unpack pattern from
    # the first line continaining only hyphens and underscores
    #
    my $bof = tell $fh;
    while (<DATA>) {
    chomp;
    if (tr/- //c == 0 and tr/-// > 0) {
    while (/-+/g) {
    my ($beg, $len) = ($-[0], $+[0] - $-[0]);
    $format .= "\@$beg A$len ";
    }
    last;
    }
    }

    warn qq(Data will be upacked with a format of "$format"\n\n);

    # Now rewind to the beginning of the file, ignore anything that contains only
    # whitespace, and unpack every record according to the pattern that we just built
    #
    seek $fh, $bof, SEEK_SET;
    while (<$fh>) {
    next unless /\S/;
    my @data = unpack $format;
    print join ',', @data;
    print "\n";
    }


    __DATA__


    CTX Destination Enabled Connection Sync'ed-as-of-time
    --- --------------------------------------------------------------------- ------- ---------------- ------------------
    1 pool://ZABRYDD01.localdomain/RDP_NEW_REP yes Sat Jun 18 08:56 Fri Jun 24 06:37
    2 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101 yes Sat Jun 18 08:57 Fri Jun 24 08:01
    4 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    5 pool://ZARDPDD01.localdomain/BRYREP yes Sat Jun 18 08:57 Fri Jun 24 07:01
    8 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    10 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    12 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    13 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60 yes Sat Jun 18 08:57 Fri Jun 24 09:00
    --- --------------------------------------------------------------------- ------- ---------------- ------------------

    **OUTPUT**

    Data will be upacked with a format of "@0 A3 @6 A69 @78 A7 @88 A16 @107 A18 "

    "CTX","Destination","Enabled","Connection","Sync'ed-as-of-time"
    "---","---------------------------------------------------------------------","-------","----------------","------------------"
    "1","pool://ZABRYDD01.localdomain/RDP_NEW_REP","","Sat Jun 18 08:56","Fri Jun 24 06:37"
    "2","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101","yes","Sat Jun 18 08:57","Fri Jun 24 08:01"
    "4","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "5","pool://ZARDPDD01.localdomain/BRYREP","yes","Sat Jun 18 08:57","Fri Jun 24 07:01"
    "8","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "10","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "12","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "13","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "---","---------------------------------------------------------------------","-------","----------------","------------------"
  • Wernher Eksteen at Jun 26, 2011 at 12:32 pm
    Hi Rob,

    Once again your expertise and willingness to help is astounding.

    I appreciate the idea that everyone else is trying to force me into learning
    a new concept I have never
    encountered before by giving me pointers in the direction such as telling me
    to look at pack and unpack
    or the CSV related modules, which are all good, but I found that often
    learning through example and then
    take what I've learned and expand on that going forth usually works for me.

    The fact that you went the extra mile and actually gave me a real example
    based on what I need, not
    only do I learn from this, but I also grasp it much quicker than having to
    first learn the entire new concept
    such as pack, unpack or the CSV related modules, but I also now have a very
    good understanding from
    which I can build on and have a good base from where I can dig deeper from
    here.

    Thank you very much for your assistance and literally showing me the ropes
    and even explaining it so
    that Perl newbies such as myself can understand it.

    Perl is a great language and a great tool that is very useful in a wide
    range of things, but grasping it's
    power is often not easy for newcomers who want to know it, but don't always
    have the time to reinvent
    the wheel, which is why standing on the shoulders of giants who does
    understand these things and who
    are willing to share their expertise and guide newcomers such as myself so
    that many more in future can
    eventually do what you do and so have a "return of investment" by helping
    the Perl community grow even
    faster as to keep it obscured and out of reach for the norm.

    Personally, I am a Java developer and Linux Admin of profession and don't
    always have the time as much
    as I would love to have digging into the finer details and aspects of
    things, and understand that I will not and
    cannot possibly know everything there is to know about any particular
    subject, but know there are people such
    as yourself out there willing to lend a hand and point those such as myself
    not only in the right direction, but also
    providing the map to get there. All I need to do is follow the road, and
    then build my own maps based on what
    I have learned from people such as yourself.

    Regards,
    Wernher
    On 25 June 2011 19:02, Rob Dixon wrote:
    On 24/06/2011 08:45, Wernher Eksteen wrote:

    I've attached a text file containing the original and required
    format and avoid the format being lost by just pasting it in the
    email body.

    The original format is separated by a space, but need to replace the
    space with a comma, so it will become a comma delimited csv file
    which I will then import to MS Excel.

    I'm not sure how to parse the "Connection" and "Sync'ed-as-of-time"
    columns since the dates there in is in this format "Fri Jun 24
    06:37" which also separated by space, but the spaces in those
    columns shouldn't be replaced by a comma. The date format in those
    columns should remain the same.

    Also, is it possible to convert this directly into a MS Excel
    document using Perl?
    Hello again Wernher.

    My first guess for this problem was to build an 'unpack' format based on
    the row of hyphens beneath the header captions. As it may still be
    useful, and well worth studying for similar applications, here is my
    alternative solution doing just that.

    The program grabs everything in the file that is aligned with the row
    of hyphens beneath the captions. It works by finding the first line in
    the file that contains only hyphens or spaces, and scanning that to
    build an unpack string using the @- and @+ arrays that hold the offsets
    of the start and end of the previous successful regex match.

    Unlike my previous solution, empty (all-space) fields are handled
    correctly: the 'A' unpack format discards trailing spaces, and my only
    proviso is that if you ever expect leading spaces in fields they must be
    trimmed explicitly.

    HTH,

    Rob


    use strict;
    use warnings;

    use Fcntl 'SEEK_SET';

    my $format;

    my $fh = *DATA; # Replace with the appropriate 'open my $fh, '<', ... or
    die $!;

    # Remember the where the file begins, and then build the unpack pattern
    from
    # the first line continaining only hyphens and underscores
    #
    my $bof = tell $fh;
    while (<DATA>) {
    chomp;
    if (tr/- //c == 0 and tr/-// > 0) {
    while (/-+/g) {
    my ($beg, $len) = ($-[0], $+[0] - $-[0]);
    $format .= "\@$beg A$len ";
    }
    last;
    }
    }

    warn qq(Data will be upacked with a format of "$format"\n\n);

    # Now rewind to the beginning of the file, ignore anything that contains
    only
    # whitespace, and unpack every record according to the pattern that we just
    built
    #
    seek $fh, $bof, SEEK_SET;
    while (<$fh>) {
    next unless /\S/;
    my @data = unpack $format;
    print join ',', @data;
    print "\n";
    }


    __DATA__


    CTX Destination
    Enabled Connection Sync'ed-as-of-time
    --- ---------------------------------------------------------------------
    ------- ---------------- ------------------
    1 pool://ZABRYDD01.localdomain/RDP_NEW_REP
    yes Sat Jun 18 08:56 Fri Jun 24 06:37
    2 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101
    yes Sat Jun 18 08:57 Fri Jun 24 08:01
    4 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22
    yes Sat Jun 18 08:57 Fri Jun 24 09:00
    5 pool://ZARDPDD01.localdomain/BRYREP
    yes Sat Jun 18 08:57 Fri Jun 24 07:01
    8 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31
    yes Sat Jun 18 08:57 Fri Jun 24 09:00
    10 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421
    yes Sat Jun 18 08:57 Fri Jun 24 09:00
    12 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5
    yes Sat Jun 18 08:57 Fri Jun 24 09:00
    13 dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60
    yes Sat Jun 18 08:57 Fri Jun 24 09:00
    --- ---------------------------------------------------------------------
    ------- ---------------- ------------------

    **OUTPUT**

    Data will be upacked with a format of "@0 A3 @6 A69 @78 A7 @88 A16 @107 A18
    "

    "CTX","Destination","Enabled","Connection","Sync'ed-as-of-time"

    "---","---------------------------------------------------------------------","-------","----------------","------------------"
    "1","pool://ZABRYDD01.localdomain/RDP_NEW_REP","","Sat Jun 18 08:56","Fri
    Jun 24 06:37"
    "2","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101","yes","Sat
    Jun 18 08:57","Fri Jun 24 08:01"
    "4","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22","yes","Sat
    Jun 18 08:57","Fri Jun 24 09:00"
    "5","pool://ZARDPDD01.localdomain/BRYREP","yes","Sat Jun 18 08:57","Fri Jun
    24 07:01"
    "8","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31","yes","Sat
    Jun 18 08:57","Fri Jun 24 09:00"
    "10","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421","yes","Sat
    Jun 18 08:57","Fri Jun 24 09:00"
    "12","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5","yes","Sat
    Jun 18 08:57","Fri Jun 24 09:00"
    "13","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60","yes","Sat
    Jun 18 08:57","Fri Jun 24 09:00"

    "---","---------------------------------------------------------------------","-------","----------------","------------------"
  • Wernher Eksteen at Jun 28, 2011 at 5:28 pm
    Hi Rob,

    I wasn't quite sure at first what you meant by passing the file handle
    <DATA> in the while loop when $fh already existed,
    so I changed the code slightly like this:


    my $file = "file.txt";
    open(my $fh, "<", $file) or die $!;

    while (<$fh>) {

    Works like a charm, thanks again!

    Regards,
    Wernher
    Rob


    use strict;
    use warnings;

    use Fcntl 'SEEK_SET';

    my $format;

    my $fh = *DATA;  # Replace with the appropriate 'open my $fh, '<', ... or die $!;

    # Remember the where the file begins, and then build the unpack pattern from
    # the first line continaining only hyphens and underscores
    #
    my $bof = tell $fh;
    while (<DATA>) {
    chomp;
    if (tr/- //c == 0 and tr/-// > 0) {
    while (/-+/g) {
    my ($beg, $len) = ($-[0], $+[0] - $-[0]);
    $format .= "\@$beg A$len ";
    }
    last;
    }
    }

    warn qq(Data will be upacked with a format of "$format"\n\n);

    # Now rewind to the beginning of the file, ignore anything that contains only
    # whitespace, and unpack every record according to the pattern that we just built
    #
    seek $fh, $bof, SEEK_SET;
    while (<$fh>) {
    next unless /\S/;
    my @data = unpack $format;
    print join ',', @data;
    print "\n";
    }


    __DATA__


    CTX   Destination                                                             Enabled   Connection         Sync'ed-as-of-time
    ---   ---------------------------------------------------------------------   -------   ----------------   ------------------
    1     pool://ZABRYDD01.localdomain/RDP_NEW_REP                                yes       Sat Jun 18 08:56   Fri Jun 24 06:37
    2     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101    yes       Sat Jun 18 08:57   Fri Jun 24 08:01
    4     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22     yes       Sat Jun 18 08:57   Fri Jun 24 09:00
    5     pool://ZARDPDD01.localdomain/BRYREP                                     yes       Sat Jun 18 08:57   Fri Jun 24 07:01
    8     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31     yes       Sat Jun 18 08:57   Fri Jun 24 09:00
    10    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421    yes       Sat Jun 18 08:57   Fri Jun 24 09:00
    12    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5      yes       Sat Jun 18 08:57   Fri Jun 24 09:00
    13    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60     yes       Sat Jun 18 08:57   Fri Jun 24 09:00
    ---   ---------------------------------------------------------------------   -------   ----------------   ------------------

    **OUTPUT**

    Data will be upacked with a format of "@0 A3 @6 A69 @78 A7 @88 A16 @107 A18 "

    "CTX","Destination","Enabled","Connection","Sync'ed-as-of-time"
    "---","---------------------------------------------------------------------","-------","----------------","------------------"
    "1","pool://ZABRYDD01.localdomain/RDP_NEW_REP","","Sat Jun 18 08:56","Fri Jun 24 06:37"
    "2","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101","yes","Sat Jun 18 08:57","Fri Jun 24 08:01"
    "4","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "5","pool://ZARDPDD01.localdomain/BRYREP","yes","Sat Jun 18 08:57","Fri Jun 24 07:01"
    "8","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "10","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "12","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "13","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60","yes","Sat Jun 18 08:57","Fri Jun 24 09:00"
    "---","---------------------------------------------------------------------","-------","----------------","------------------"

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

  • Rob Dixon at Jun 28, 2011 at 8:08 pm

    On 28/06/2011 18:28, Wernher Eksteen wrote:

    Rob


    use strict;
    use warnings;

    use Fcntl 'SEEK_SET';

    my $format;

    my $fh = *DATA; # Replace with the appropriate 'open my $fh, '<', ... or die $!;
    I wasn't quite sure at first what you meant by passing the file handle
    <DATA> in the while loop when $fh already existed,
    so I changed the code slightly like this:


    my $file = "file.txt";
    open(my $fh, "<", $file) or die $!;

    while (<$fh>) {

    Works like a charm, thanks again!
    That's exactly right. I meant that, if you were using an external file,
    you only needed to replace the line

    my $fh = *DATA;

    with

    open my $fh, '<', 'myfile.txt' or die $!;

    which is pretty much what you have done. Unfortunately I made a mistake
    and wrote

    while (<DATA>) {

    in the first loop, when it should be the same as the second loop which reads

    while (<$fh>) {

    My apologies.

    Rob
  • Wernher Eksteen at Jun 28, 2011 at 9:04 pm

    That's exactly right. I meant that, if you were using an external file,
    you only needed to replace the line

    my $fh = *DATA;

    with

    open my $fh, '<', 'myfile.txt' or die $!;

    which is pretty much what you have done. Unfortunately I made a mistake
    and wrote

    while (<DATA>) {

    in the first loop, when it should be the same as the second loop which
    reads

    while (<$fh>) {

    My apologies.

    Rob
    No apologies necessary, I'm just very happy that I could figure it out and
    resolve it without any help :)

    And so I'm a little bit wiser thanks to your help! Like someone said before,
    Rob you rock!

    Wernher

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 24, '11 at 7:45a
activeJun 28, '11 at 9:04p
posts11
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase