Multiple substr() exchange

Discussion in 'Perl Misc' started by dima@inotech.ru, Sep 2, 2005.

  1. Guest

    Hello All!
    I have following problem:
    The file contain structured lines
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB DDDDD
    BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    I want save info into hash
    For example for third line
    $hash{one} = AAAAA
    $hash{two} = undef
    $hash{three} = CCCCC
    $hash{four} = DDDDD

    I know how do that using substr(), but this is not very elegantly!

    Using regular expressions or split is inpossible, because lines contain
    empty elements!!!

    What solution is optimal for me???
    Maybe any CPAN modules???
     
    , Sep 2, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > Hello All!
    > I have following problem:
    > The file contain structured lines
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA BBBBB DDDDD
    > BBBBB CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > I want save info into hash
    > For example for third line
    > $hash{one} = AAAAA
    > $hash{two} = undef
    > $hash{three} = CCCCC
    > $hash{four} = DDDDD
    >
    > I know how do that using substr(), but this is not very elegantly!
    >
    > Using regular expressions or split is inpossible, because lines contain
    > empty elements!!!


    You have an odd definition of "impossible".

    Loop through the line, examine the each set of 5 characters. If any of
    them are not a space character, put that string in the hash.
    Otherwise, put undef in the hash.

    > What solution is optimal for me???


    Here's one to get you started. Modify as needed:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;

    my @lines;
    while (<DATA>){
    my %line;
    @line{qw/one two three four/} = map { /\S/ ? $_ : undef }
    /(.{5}).?/g;
    push @lines, \%line;
    }

    print Dumper(\@lines);


    __DATA__
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB DDDDD
    BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD


    Output:
    $VAR1 = [
    {
    'one' => 'AAAAA',
    'three' => 'CCCCC',
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    },
    {
    'one' => 'AAAAA',
    'three' => 'CCCCC',
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    },
    {
    'one' => 'AAAAA',
    'three' => 'CCCCC',
    'two' => undef,
    'four' => 'DDDDD'
    },
    {
    'one' => 'AAAAA',
    'three' => 'CCCCC',
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    },
    {
    'one' => 'AAAAA',
    'three' => undef,
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    },
    {
    'one' => undef,
    'three' => 'CCCCC',
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    },
    {
    'one' => 'AAAAA',
    'three' => 'CCCCC',
    'two' => 'BBBBB',
    'four' => 'DDDDD'
    }
    ];


    Paul Lalli
     
    Paul Lalli, Sep 2, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Hello All!
    > I have following problem:
    > The file contain structured lines
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > AAAAA BBBBB DDDDD
    > BBBBB CCCCC DDDDD
    > AAAAA BBBBB CCCCC DDDDD
    > I want save info into hash
    > For example for third line
    > $hash{one} = AAAAA
    > $hash{two} = undef
    > $hash{three} = CCCCC
    > $hash{four} = DDDDD


    I'd use an array for that, not a hash, but that's a minor detail.

    > I know how do that using substr(), but this is not very elegantly!


    If you find yourself calling substr() on the same string a lot, consider
    using unpack(). Fixed-format data is likewise an invitation to use
    unpack().

    > Using regular expressions or split is inpossible, because lines contain
    > empty elements!!!


    You certainly *could* use a regex (and probably a tricky split too),
    but unpack() is the tool for the job.

    #!/usr/bin/perl
    use strict; use warnings; $| = 1;

    use Data::Dumper;

    while ( <DATA> ) {
    my %h;
    @h{ qw( one two three four)} = unpack 'a5xa5xa5xa5', $_;
    $_ eq ' ' and $_ = undef for values %h; # blank fields
    print Dumper \ %h;
    }

    __DATA__
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD
    AAAAA BBBBB DDDDD
    BBBBB CCCCC DDDDD
    AAAAA BBBBB CCCCC DDDDD

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. www.westerncartooncards.ca

    Exchange Links < Western Cartoon Cards > Exchange Links

    www.westerncartooncards.ca, Jul 12, 2004, in forum: HTML
    Replies:
    2
    Views:
    1,292
    Toby Inkster
    Jul 12, 2004
  2. Kees Hoogendijk

    getline and substr

    Kees Hoogendijk, Dec 20, 2003, in forum: C++
    Replies:
    4
    Views:
    575
    Kees Hoogendijk
    Dec 21, 2003
  3. entropy123

    [C++] Odd Problem with "substr"

    entropy123, Jun 1, 2005, in forum: C++
    Replies:
    7
    Views:
    559
    Stephen Howe
    Jun 2, 2005
  4. David Resnick
    Replies:
    1
    Views:
    325
    Victor Bazarov
    Apr 10, 2006
  5. Alessandro Barracco

    matrix: exchange rows + exchange columns

    Alessandro Barracco, Jan 25, 2011, in forum: Ruby
    Replies:
    0
    Views:
    178
    Alessandro Barracco
    Jan 25, 2011
Loading...

Share This Page