Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

extracting data from a file

by indy07 (Initiate)
on Dec 14, 2012 at 03:51 UTC ( #1008762=perlquestion: print w/replies, xml ) Need Help??
indy07 has asked for the wisdom of the Perl Monks concerning the following question:

I have this file that contains the following info; "qazwsx" "edcrfv" "tgbyhn" I need to extract the characters between the " marks place them into seperate scalars. How would I do that in perl?

Replies are listed 'Best First'.
Re: extracting data from a file
by davido (Archbishop) on Dec 14, 2012 at 04:15 UTC

    I'm not comfortable that your description of the data-set is complete enough to provide a answer with any degree of confidence that it will "just work." But assuming you've got quoted fields delimited by a space character, use Text::CSV, and set sep_char => q{ }, # 0x20.

    CPAN is how you "do that in Perl."

    Update: An example is in order:

    use Text::CSV; my $csv = Text::CSV->new( { sep_char => q/ /, # 0x20 escape_char => q/\\/, # Single backslash escapes. } ); while( my $row = $csv->getline(\*DATA) ) { print '[', join( '] [', @{$row} ), "]\n"; } __DATA__ "qazwsx" "edcrfv" "tgbyhn" "asdf" "ghjk" "l;zx" "cv\"bn"

    ...which will produce the following output...

    [qazwsx] [edcrfv] [tgbyhn] [asdf] [ghjk] [l;zx] [cv"bn]

    I threw in an escaped quote character just for fun.


Re: extracting data from a file
by frozenwithjoy (Priest) on Dec 14, 2012 at 07:17 UTC
    You could just capture stretches of non-quote characters that are between quotes:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $string = q("qazwsx" "edcrfv" "tgbyhn"); my @array = $string =~ /"([^"]*)"/g; say $_ for @array; __END__ qazwsx edcrfv tgbyhn
Re: extracting data from a file
by Anonymous Monk on Dec 14, 2012 at 07:29 UTC

    Start with Text::CSV esp with these programs that are in the Text::CSV_XS distribution
    examples/csv-check Script to check a CSV file/stream
    examples/csvdiff Script to shoff diff between sorted CSV files
    examples/ Parse CSV stream, be forgiving on bad lines
    examples/ Small benchmark script

Re: extracting data from a file
by nithins (Sexton) on Dec 14, 2012 at 07:21 UTC

    moving each word in to a variable that too dynamically creating it during the run time is not possible by the best of knowledge i know

    but u can dump it in to an hash which will create key value same as that of characters b/w " "

    hope this code might help u

    #!/usr/bin/perl use strict; use Data::Dumper; open ( FH ,'<' , 'D:\\myperlpgm\\nperl\\newfile.txt'); my %has; my @array; while(<FH>){ @array = split /"/; } print "@array\n"; for(@array){ $has{$_} = $_ ; } print Dumper ($has{ssss});

    D:\\myperlpgm\\nperl\\newfile.txt contains -> "nithn""nnnn""ssss" data


      What is the hash for?

      And take a closer look at what's in @array. By changing "print "@array\n"; to "print Dumper @array;", you will see that @array contains extra elements that result from splitting on the quote character as if it were a simple delimiter rather than a balanced construct.

      Also, given the OP's data set, there's no reason for the $hash{ssss} element to even exist.

      This is a broken solution.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008762]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2018-06-19 15:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.