Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Transforming a scalar to an array

by vxp (Pilgrim)
on Jul 15, 2009 at 14:49 UTC ( [id://780342]=perlquestion: print w/replies, xml ) Need Help??

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

Suppose you have this input:

line1 line2 line3 line4 line5

And the following code:

#!/usr/bin/perl my $file = shift; local( $/, *INPUT ); open(INPUT, $file); $input = <INPUT>; print $input;

Is there a way to transform that $input scalar into an array? I realize that local( $/, *INPUT ); gets rid of the new lines. I also don't have any special separators in my actual input (my "real" input is a lot like this "fake" input, except that its MIME encoded, and I decode it using MIME::QuotedPrint::Perl. Which by the way, is why I'm doing local( $/, *INPUT ); in the first place.. I have to read the whole MIME-encoded file into a scalar to decode it. Can't read it in line by line because of that. :/ )

I just want to iterate through $input line by line and do something with each..so, somehow, I need to turn it into an array :)

Any thoughts/suggestions/solutions welcome!

Replies are listed 'Best First'.
Re: Transforming a scalar to an array
by ikegami (Patriarch) on Jul 15, 2009 at 14:59 UTC
    #!/usr/bin/perl use strict; use warnings; chomp( my @input = <> );

    Update: Oh, it seems you aren't reading from a file handle as you showed.

    chomp( my @input = split(/^/m, $input) );
Re: Transforming a scalar to an array
by davorg (Chancellor) on Jul 15, 2009 at 15:04 UTC
    I realize that local( $/, *INPUT ); gets rid of the new lines.

    It doesn't actually. You end up with a scalar that still has a newline at the end of each line of text. You can, of course, use that to your advantage.

    my @lines = split /\n/, $input;
    --

    See the Copyright notice on my home node.

    Perl training courses

      That's different from <> in that it will trim trailing blank lines (which is probably fine) and chomp all lines (which is probably fine).
Re: Transforming a scalar to an array
by Anonymous Monk on Jul 15, 2009 at 15:07 UTC
    sure, use split

    now you just have to know where to split, since you don't have any newlines..
      ... since you don't have any newlines.
      But the newlines are all still there: they are embedded within the single string that represents the data read from the entire file.
Re: Transforming a scalar to an array
by vxp (Pilgrim) on Jul 15, 2009 at 15:57 UTC

    Thanks, all!

    All's well now

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found