Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Array function to concatenate

by dwm042 (Priest)
on Oct 10, 2008 at 15:21 UTC ( [id://716475]=note: print w/replies, xml ) Need Help??


in reply to Array function to concatenate

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 = ("http://software.com","http://yahoo.com"); 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 array_cat.pl 404 http://software.com Not found 408 http://yahoo.com Request Timeout

Replies are listed 'Best First'.
Re^2: Array function to concatenate
by johngg (Canon) on Oct 10, 2008 at 21:21 UTC
    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{ http://software.com http://yahoo.com }; my @Text = ( q{Not found}, q{Request Timeout} ); print map { qq{$Code[$_] $Portal_Name[$_] $Text[$_]\n} } 0 .. $#Portal_Name;

    The output.

    404 http://software.com Not found 408 http://yahoo.com Request Timeout

    I hope this is of interest.

    Cheers,

    JohnGG

      Thanks all for your help..Its realy interesting to know different ways of getting info. Regards, Gowri
Re^2: Array function to concatenate
by Zen (Deacon) on Oct 10, 2008 at 16:49 UTC
    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://716475]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found