Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Array function to concatenate

by gowrisub (Initiate)
on Oct 10, 2008 at 13:55 UTC ( #716454=perlquestion: print w/replies, xml ) Need Help??

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

I have to create a set of data using an array ftn for the variables content code, portal name, content text and display the information in a continous text form. Pls let me know how to concatenate the corresponing indexes. ------------------ @Code = (404,408); @Portal_Name = ("",""); @Text = ("Not found","Request Timeout"); push(@Portal_Name,@Code); push(@Portal_Name,@Text); print @Portal_Name; -------------- I want to print 404 Notfound like that for each index element

Replies are listed 'Best First'.
Re: Array function to concatenate
by Fletch (Bishop) on Oct 10, 2008 at 14:09 UTC

    a) use <code></code> tags. 2) See How (Not) To Ask A Question. III) Rather than keeping three parallel data structures like that it might make more sense to store the data for the same entry in one place (e.g. an array of hashrefs, each hashref containing the code, name, and text; see perldsc, perllol, perlreftut).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Array function to concatenate
by suaveant (Parson) on Oct 10, 2008 at 14:05 UTC
    If you know they are all arrays with corresponding indexes it is easy.
    for(my $i=0; $i<@Code; $i++) { print "$Code[$i] $Portal_Name[$i] $Text[$i]\n"; }

                    - Ant
                    - Some of my best work - (1 2 3)

      for my $i ( 0..$#Code ) { print "$Code[$i] $Portal_Name[$i] $Text[$i]\n"; }
Re: Array function to concatenate
by dwm042 (Priest) on Oct 10, 2008 at 15:21 UTC
    I'm sure there is a pretty solution with map but this is what i came up with. Using sprintf makes it easier to switch formatting as required.

    #!/usr/bin/perl use warnings; use strict; my @Code = (404,408); my @Portal_Name = ("",""); my @Text = ("Not found","Request Timeout"); for my $i ( 0 .. $#Portal_Name ) { $Portal_Name[$i] = sprintf "%s %s %s\n", $Code[$i], $Portal_Name[$ +i], $Text[$i]; } for( @Portal_Name ) { print; }
    And the output is:

    C:\Code>perl 404 Not found 408 Request Timeout
      I'm sure there is a pretty solution with map but ...

      I'm not sure about pretty but it is fairly straightforward.

      In your code, as well as the slightly dubious use of sprintf, I wonder why you trample over @Portal_Name and use two for loops when you could have done a print (or even a printf if you insist) in the first loop.

      use strict; use warnings; my @Code = qw{ 404 408 }; my @Portal_Name = qw{ }; my @Text = ( q{Not found}, q{Request Timeout} ); print map { qq{$Code[$_] $Portal_Name[$_] $Text[$_]\n} } 0 .. $#Portal_Name;

      The output.

      404 Not found 408 Request Timeout

      I hope this is of interest.



        Thanks all for your help..Its realy interesting to know different ways of getting info. Regards, Gowri
      Not seeing the advantage of sprintf here; perl would print it correctly anyway. Can you expand on why it wins (or e.g. why print fails)?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://716454]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-12-08 12:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found