Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Split array and join with results

by BillKSmith (Deacon)
on Aug 22, 2012 at 20:30 UTC ( #989138=note: print w/ replies, xml ) Need Help??

in reply to Split array and join with results

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


Comment on Re: Split array and join with results
Replies are listed 'Best First'.
Re^2: Split array and join with results
by perlnoobster (Sexton) on Aug 22, 2012 at 21:02 UTC
    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.

        on further inspection it seem as though its the $colr value in the print out function that seems to be inserting the line break?  print OUT "\n$phn $colr $title\n"; Please can someone help me?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-10-09 16:08 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (244 votes), past polls