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

Hi guys, Inside the array two variables had to be stored. The length from the starting of the first variable to the start of the second variable should be always constant.
Example:

Let us consider the first variable will never exceed 8 characters, but may vary from 1 character to 8. Now the remaining length after the end of the first variable should be accupied by spaces.
a[0] = value1 name1, a[1] = val name2, a[2] = value name3, a[3] = v name4

Even in the above example the multiple spaces between the variables are not displaying properly as i wished.

I hope every one understands my issue. If not please let me know
Is this is possible by any way. Thank you perlmonks.

2002-07-12 Edit by zdog: Added CODE tags

Replies are listed 'Best First'.
Re: format with space
by amphiplex (Monk) on Jul 12, 2002 at 13:50 UTC
    Hi !

    I don't really understand what you are trying to do, but sprintf might prove useful:
    $s = sprintf "%-8s%s", "val1", "name1"; print $s."\n";
    ---- kurt
      Thanks guys! This is what i was trying to do. "sprintf" -- is the function best for my issue. Thank you perlmonks
      Thanks guys! Yeap i do no that i am not able to explain properly. But you guys understood somewhat and helped me. Cool. This helps me to solve my problem. I do not know about this function sprintf. "sprintf" -- is the function best for my issue. Thank you perlmonks
(zdog) Re: format with space
by zdog (Priest) on Jul 12, 2002 at 13:52 UTC
    I think you're asking the same thing that I did here, a while back. If that's the case, I suggest looking into sprintf and possibly doing something like this to pad the value:

    a[0] = sprintf ("%-8s%s", $value, $name)

    I think that's what you're getting at ...

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re: format with space
by Abigail-II (Bishop) on Jul 12, 2002 at 13:43 UTC
    What exactly is your question? I guess you have a problem somewhere, but you are not explaining what it is. What did you try, and where did it fail?

    As for not displaying properly, the amount of spaces looks correct to me.

    Abigail

Re: format with space
by Fletch (Bishop) on Jul 12, 2002 at 14:01 UTC

    Ambiguity aside, it really looks like you'd be better off using a proper data structure (arrayrefs and having a list of lists, see perldoc perllol) rather than kludging something with fixed widths.

Re: format with space
by bronto (Priest) on Jul 12, 2002 at 15:29 UTC

    If I understand what your need is, this approrach should work for you: this script encodes and decodes back an array of values similar to yours:

    use strict ; use warnings ; my @data = qw(la marianna la va in campagna quando il sole tramontera`) ; my @results ; while (my @couple = splice @data,0,2) { push @results,pack("A8A*",@couple) ; } print "$_\n" foreach @results ; my @decoded ; push @decoded,unpack("A8A*",$_) foreach @results ; print "[$_]\n" foreach @decoded ;

    This prints

    la marianna la va in campagna quando il sole tramontera` [la] [marianna] [la] [va] [in] [campagna] [quando] [il] [sole] [tramontera`]

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Is the problem format or data structure?
by BorgCopyeditor (Friar) on Jul 12, 2002 at 18:56 UTC

    Let's see ... a list of name-value pairs ... to be printed in columns. Since people have already mentioned print formatting methods, let me just add what leapt out at me when I saw this:

    Hash!

    The issue of printing these pairs out in columns should be independent of how you store the data, no? One is structure, one is presentation.

    BCE
    --Your punctuation skills are insufficient!