Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Manipulating Output

by Anonymous Monk
on Jul 09, 2002 at 14:31 UTC ( #180489=perlquestion: print w/replies, xml ) Need Help??

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

I need to find out how to get an output from a file with a : delimeter. I want to just get the Server name here.

So my output right now is:
I would like it to be:
firstServer secondServer
here is my script as of now:
@a = (<DATA>); $file = "bas.txt"; foreach (@a) { open(FILE,">>$file") || die "Can't open $file: $! \n"; print FILE "$_\n"; } close(FILE); close(DATA); __DATA__ 222.222.333.444:firstServer

Replies are listed 'Best First'.
Re: Manipulating Output
by Abigail-II (Bishop) on Jul 09, 2002 at 14:34 UTC
    print FILE (split /:/ => $_) [-1], "\n";
    Question: why do you open the file in each iteration? Opening it once is more efficient.


      thanks. Sorry I only gave just a small portion of my script and you answered my question. Now that it works can you please explain how this works???? I am lost on the => and the [-1] parts.

      print FILE (split /:/ => $_) [-1], "\n";
        => is a fancy comma, and [-1] takes the last element of the list preceeding it.


Re: Manipulating Output
by ehdonhon (Curate) on Jul 09, 2002 at 15:00 UTC

    Abigail-II has the correct perl solution. However, if all you are doing is writing a very small perl program for that purpose alone, there's no need to re-invent the wheel.

    cut -f 2 -d ':' data_file > output_file
      Or a perl one-liner:
      perl -aF: -ple'$_=pop@F' in_file > out_file


      Here are a few ways to do it if you want to do it from the command line in Perl:
      perl -pe 's/[^:]+://' data_file > output_file perl -nle 'print /([^:]+)$/' data_file > output_file perl -nle 'print+(split /:/)[-1]' data_file > output_file
(zdog) Re: Manipulating Output
by zdog (Priest) on Jul 09, 2002 at 15:56 UTC
    Here are a few comments on your existing code:

    use strict; # always 'use strict' use warnings; # always 'use warnings' or the -w switch chomp (my @a = <DATA>); # chomp the array to get rid of extra \n's my $file = "bas.txt"; # get the open() out of the loop so it's not repeated unnecessarily open (FILE, ">>$file") || die "Can't open $file: $! \n"; print FILE "$_\n" for @a; # shorter; sweeter close(FILE); close(DATA); __DATA__ 222.222.333.444:firstServer

    I know that I'm not answering your question (it's been done), but those are a few other things to keep in mind for the future.

    Zenon Zabinski | zdog |

Re: Manipulating Output
by hiseldl (Priest) on Jul 09, 2002 at 15:16 UTC
    if the data file exists and you want to keep the same filename (this will keep the original with .bak suffix):
    > perl -pi.bak -e "s/^.*://" data.txt
    or if you would like to create a new file to hold the output:
    > perl -pe "s/^.*://" data.txt > output.txt

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-02-03 23:16 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (30 votes). Check out past polls.