Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Split array and join with results

by BillKSmith (Chaplain)
on Aug 23, 2012 at 04:01 UTC ( #989190=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Split array and join with results
in thread Split array and join with results

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


Comment on Re^3: Split array and join with results
Download Code
Re^4: Split array and join with results
by perlnoobster (Sexton) on Aug 23, 2012 at 10:51 UTC
    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?

      $colr gets its value (including the newline) from @colours. If you prefer, you can chomp (refer perldoc -f chomp) $colr before you print it.

      Bill
        Hi Bill, Thank you so much for helping me, I really appreciate it! I've added the chomp function however i get the following error in secureCRT Modification of a read-only value attempted?? I think i've done it correctly:
        foreach my $colr (@COLOURS) { chomp $colr; print OUT "\n$phn $colr $title\n"; }
        How can i fix this? where do i place the chomp function? Thank you :)

Log In?
Username:
Password:

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

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

    How do you remember the number of days in each month?











    Results (223 votes), past polls