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

Hi

I have the following strings in a file:
"HP LaserJet IIISi" {set model "net_ljx000"} "HP PaintJet XL300" {set model "net_dsnj"} "HP LaserJet 4 Plus / 4M Plus" {set model "net_lj4x"} "HP LaserJet 5L" {set model "net_lj4x"} "HP LaserJet 5MP" {set model "net_lj4x"} "HP LaserJet 5P" {set model "net_lj4x"}
*note the spaces in front of the string I want to print it out as:
modelCombo.addItem("HP LaserJet IIISi"); modelCombo.addItem("HP PaintJet XL300"); modelCombo.addItem("HP LaserJet 4 Plus / 4M Plus"); modelCombo.addItem("HP LaserJet 5L"); modelCombo.addItem("HP LaserJet 5MP"); modelCombo.addItem("HP LaserJet 5P");
I tried with the following:
open( IN, "print_tools.txt" )or die("Cannot open print_tools.txt for r +eading: $!" ); while(<IN>) { print "modelCombo.addItem($_)\n" if s/{\w+\W+\w+\W+\w+\W?}//g; + }
What I got was:
modelLabel.addItem( "HP LaserJet IIISi" )modelLabel.addItem( "HP PaintJet XL300" )modelLabel.addItem( "HP LaserJet 4 Plus / 4M Plus" )modelLabel.addItem( "HP LaserJet 5L" )modelLabel.addItem( "HP LaserJet 5MP" )modelLabel.addItem( "HP LaserJet 5P" )
Any suggestions on how I could fix my code.

--kirk123

Edit: Added <code> tags. larsen

Replies are listed 'Best First'.
Re: Problem with string manipulation
by tachyon (Chancellor) on Jan 03, 2003 at 22:45 UTC

    If you stand back from the problem what you want to match is the entire string between the first pair or " chars ie m/"([^"]+)"/ so all you need is:

    while ( my $line = <DATA> ) { print qq!modelCombo.addItem("$1");\n! if $line =~ m/"([^"]+)"/; } __DATA__ "HP LaserJet IIISi" {set model "net_ljx000"} "HP PaintJet XL300" {set model "net_dsnj"} "HP LaserJet 4 Plus / 4M Plus" {set model "net_lj4x"} "HP LaserJet 5L" {set model "net_lj4x"} "HP LaserJet 5MP" {set model "net_lj4x"} "HP LaserJet 5P" {set model "net_lj4x"}<p>

    If the space has some significance and you have other format records, or you want to capture the second part as well you could make that m/^\s+"([^"]+)"\s*\{set model "([^"]+)"\}/ which is rather more specific for that record structure.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Problem with string manipulation
by gjb (Vicar) on Jan 03, 2003 at 22:50 UTC

    Or even shorter if you're sure the model names don't contains escaped quotes:

    while (<DATA>) { print "modelCombo.addItem($1);\n" if /^\s*(\"[^\"]+\")/; }

    Hope this helps, -gjb-

Re: Problem with string manipulation
by fruiture (Curate) on Jan 03, 2003 at 22:43 UTC

    I'd use Text::ParseWords. Maybe it's to good for the purpose, but at will never be too bad for it.

    use Text::ParseWords 'shellwords'; open my $infh , '<' , 'print_tools.txt' or die "... $!"; while( <$infh> ){ my $printer = ( shellwords($_) )[0]; print qq{modelCombo.addItem("$printer")\n}; } close $infh;
    --
    http://fruiture.de
Re: Problem with string manipulation
by Mr. Muskrat (Canon) on Jan 03, 2003 at 22:25 UTC
    This should do the trick:
    open(IN, "<", "print_tools.txt") or die "Cannot open print_tools.txt f +or reading, $!"; while(<IN>){ chomp; s/^\s+//; s/\s+{.*}//; print "modelCombo.addItem($_);\n"; }

    Updated to fully satisfy OP's requirements.

      Or, omitting the substitutions and keeping the original string intact,
      open( IN, "<", "print_tools.txt" )or die("Cannot open print_tools.txt +for reading: $!" ); while(<IN>) { /("[^"]+")/; print "modelCombo.addItem($1);\n"; } close IN;

      PCS
      I still got space after the string otherwise its ok. thanks. --kirk123