Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Split array and join with results

by perlnoobster (Sexton)
on Aug 22, 2012 at 14:37 UTC ( #989054=perlquestion: print w/ replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks, i'm a noob to perl and need assistance with the following dilemma, I have a list of words being read from a text file and I have an array of colours that I wish to assign to each word. The text file contains the following: iphone 3 iphone 3gs iphone 4 the array contains colours such as red blue green all i require is an output file showing the following: iphone 3 red iphone 3 blue iphone 3 green iphone 3gs red iphone 3gs blue iphone 3gs green iphone 4 red iphone 4 blue iphone 4 green

here's the coding for the array so far:
{ no warnings 'uninitialized'; chomp $results; push (@oid,$results[0]); $_ = defined($_) ? $_ : '' for @results; print "\n$results"; my $colour=(); my @colours=(); @colours = ( "baby blue", "baby pink", "black", "dark blue", "brown", + "dark purple", "green", "orange", "hot pink", "light purple", "red", + "white", "yellow"); foreach $colour (@colours) { print $colour; } print OUT "$results$colour\n" }
Please can someone help me? Thank you :)

Comment on Split array and join with results
Download Code
Re: Split array and join with results
by cheekuperl (Monk) on Aug 22, 2012 at 14:59 UTC
    If I understand it correctly, you are assigning each color to each phone.
    Reading the phones list from file into an array, I leave up to you.
    @phones=qw(iph1 iph2 iph3); @colors=qw(blue green red); foreach $phn (@phones) { foreach $colr (@colors) { print "\n$phn $colr"; } print "\n==="; }
    Does this help?
      hi, Thank you for replying! I sort of understand what you've done but the phone models are read from a text file and they can vary from anything from a few models to loads more so i cant hard code that into the code, can that be adapted into the coding? I'm currently using this array for the list of all the colours:
      @colours = ( "baby blue", "baby pink", "black", "dark blue", "brown" +, "dark purple", "green", "orange", "hot pink", "light purple", "red" +, "white", "yellow"); foreach $colour (@colours) { print $colour; }
      I hope this makes sense?
        to simply, how do i adapt the following: @phones=qw(iph1 iph2 iph3); so that it reads the phone titles from the text file:
        chomp $results; push (@oid,$results[0]); $_ = defined($_) ? $_ : '' for @results;
        I hope that makes sense?
Re: Split array and join with results
by BillKSmith (Chaplain) on Aug 22, 2012 at 20:30 UTC

    Formally, you want the cross product of the set of models and the set of colors. The post by cheekuperl shows how to do this. It does not know or care where the sets came from.

    The only remaining question is how to parse the phone file and store the models into the array @phones. You have not given us enough detail about the file to help you with this. Does it have one phone per line? If not, how are the models seperated? Should whitespace be removed?, etc

    Bill
      Hi Bill, I apologize for not being clear, cheekuperl provided a great solution however the list of phone models are in a text file called mobiles.txt there are several columns In the text file however the phone models will always be in the first column, the phone models need a color assigned to them which can be retrieved via the colpr array, the phone models are one per line in the first column some models have white space e.g Nokia 8250 or Samsung galaxy nexus etc I hope this reply is insightful?? Thank you :-)

        Put list of colours in __DATA__ where it can easily be changed. Get the name of the phone file from the command line. Extract the model from each record of the file with a regular expression. (My regexp assumes that the 'columns' are seperated by TABs.) The processing is the same as before.

        use strict; use warnings; use Readonly; # Assume input file name on command line (default is 'phones.txt). Readonly::Scalar my $PHONE_FILE => $ARGV[0] || 'phones.txt'; Readonly::Array my @COLOURS => <DATA>; # Assume columns seperated by tabs, all other whitespace is significa +nt. Readonly::Scalar my $GET_MODEL => qr{\A ( [^\t]+ ) }x; open my $PHONES, '<', $PHONE_FILE or die "Cannot open $PHONE_FILE\n"; while (my ($phn) = <$PHONES> =~ $GET_MODEL) { foreach my $colr (@COLOURS) { print "\n$phn $colr"; } print "\n==="; } close $PHONES __DATA__ baby blue baby pink black dark blue brown dark purple green orange hot pink light purple red white yellow

        Note: Readonly is not necessarey. Its purpose here is to assure the reader that those symbols are logically constants.

        Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989054]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-07-24 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (165 votes), past polls